pathelper

package module
v1.0.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 22, 2024 License: MIT Imports: 5 Imported by: 3

README

pathelper

Build Go Report Card Go Reference

pathelper is a golang package which provides path related helper functions.

Docs

Documentation

Overview

pathelper is a golang package which provides path related helper functions.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func BaseWithoutExt added in v1.0.7

func BaseWithoutExt(fileName string) string

BaseWithoutExt returns file base name without ext name.

Example
package main

import (
	"fmt"

	"github.com/northbright/pathelper"
)

func main() {
	file := "/tmp/input/data.json"

	baseWithoutExt := pathelper.BaseWithoutExt(file)
	fmt.Printf("%s\n", baseWithoutExt)

}
Output:

data

func CommandExists

func CommandExists(cmd string) bool

CommandExists returns if given command exists or not.

Example
package main

import (
	"log"

	"github.com/northbright/pathelper"
)

func main() {
	exists := pathelper.CommandExists("go")
	log.Printf("go exists: %v", exists)

}
Output:

func CreateDirIfNotExists added in v1.0.2

func CreateDirIfNotExists(dir string, perm os.FileMode) error

CreateDirIfNotExists creates the directory if it does not exists.

Example
package main

import (
	"fmt"
	"log"

	"github.com/northbright/pathelper"
)

func main() {
	dir := "test/output"
	if err := pathelper.CreateDirIfNotExists(dir, 0755); err != nil {
		log.Printf("CreateDirIfNotExists error: %v", err)
		return
	}

	exists := pathelper.FileExists(dir)
	fmt.Printf("%v\n", exists)

}
Output:

true

func ExecDir

func ExecDir() (string, error)

ExecDir returns the absolute path of current executable dir. For Go 1.18 and later, you may just use os.Executable instead.

Example

Run "go test -c && ./pathelper.test"

package main

import (
	"log"
	"path/filepath"

	"github.com/northbright/pathelper"
)

func main() {
	// Get current executable dir.
	execDir, _ := pathelper.ExecDir()
	log.Printf("executable dir: %v", execDir)

	// Make an absolute path for image dir.
	imageDir := filepath.Join(execDir, "static", "images")
	log.Printf("image dir: %v", imageDir)

}
Output:

func FileExists

func FileExists(f string) bool

FileExists returns if given file exists or not.

Example
package main

import (
	"log"

	"github.com/northbright/pathelper"
)

func main() {
	exists := pathelper.FileExists("/tmp")
	log.Printf("/tmp exists: %v", exists)

}
Output:

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL