Skip to content
Snippets Groups Projects
util.go 1.02 KiB
Newer Older
  • Learn to ignore specific revisions
  • package util
    
    import (
    	"fmt"
    	"os"
    	"path/filepath"
    	"strings"
    	"time"
    )
    
    func Now() int64 {
    
    }
    
    func GenerateGosdnPath() (string, error) {
    	var execPath string
    	var absExecPath string
    	var err error
    	if execPath, err = os.Executable(); err != nil {
    		return "", err
    	}
    	if absExecPath, err = filepath.Abs(execPath); err != nil {
    		return "", err
    	}
    	executableDir := filepath.Dir(absExecPath)
    	projectRoot := filepath.Dir(executableDir)
    
    	return projectRoot, nil
    }
    
    // Get the path of a file, if path is a path, just return absolute path
    func GetAbsPath(path string) (string, error) {
    	absPath, err := filepath.Abs(path)
    	if err != nil {
    		return "", fmt.Errorf("Failed to resolve absolut path of %s: %w", path, err)
    	}
    	if strings.HasSuffix(path, "/") {
    		return absPath, nil
    	}
    	info, err := os.Stat(absPath)
    	if err != nil {
    		return "", fmt.Errorf("Failed to stat path: %w", err)
    	}
    	// If path is not a directory, make it one
    	if !info.IsDir() {
    		absPath = filepath.Dir(absPath)
    	}
    
    	return absPath, nil
    }