Skip to content

Commit

Permalink
feat: add ConvertLinuxPath
Browse files Browse the repository at this point in the history
  • Loading branch information
fcying committed Aug 1, 2024
1 parent 224811e commit d1e66a1
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 27 deletions.
10 changes: 7 additions & 3 deletions cmd/compiledb/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ import (
"github.com/urfave/cli/v2"
)

var Version string = "v1.1.0"

func init() {
log.SetOutput(os.Stdout)
log.SetLevel(log.WarnLevel)
// log.SetLevel(log.InfoLevel)
log.SetLevel(log.WarnLevel)
}

func updateConfig(ctx *cli.Context) {
Expand All @@ -30,7 +32,8 @@ func updateConfig(ctx *cli.Context) {
}

func main() {
cli.AppHelpTemplate = `{{.HelpName}} v{{.Version}}

cli.AppHelpTemplate = `{{.HelpName}} {{.Version}}
USAGE: {{.Name}} {{if .VisibleFlags}}[options]{{end}}{{if .Commands}} command [command options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}[args]...
{{end}}
Expand All @@ -45,7 +48,7 @@ COMMANDS:
app := &cli.App{
// Compiled: time.Now()
EnableBashCompletion: true,
Version: "1.0.0",
Version: Version,
UseShortOptionHandling: true,
HideHelpCommand: true,
HideVersion: true,
Expand Down Expand Up @@ -108,6 +111,7 @@ COMMANDS:
DisableDefaultText: true,
Action: func(*cli.Context, bool) error {
log.SetLevel(log.DebugLevel)
log.Println("compiledb-go start, version:", Version)
return nil
},
},
Expand Down
19 changes: 0 additions & 19 deletions internal/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,6 @@ var (
ParseResult []interface{}
)

func FileExist(filename string) bool {
_, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return true
}

func GetBinFullPath(name string) string {
log.Println(name)
path, err := exec.LookPath(name)
if err != nil {
log.Println("Error finding program:", err)
return ""
}
log.Println(path)
return path
}

func WriteJSON(filename string) {
if CommandCnt == 0 {
return
Expand Down
10 changes: 5 additions & 5 deletions internal/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,14 @@ func Parse(buildLog []string) {
if ParseConfig.BuildDir != "" {
workingDir = ParseConfig.BuildDir
} else {
workingDir, err = os.Getwd()
if err != nil {
log.Fatalf("get workingDir failed! %v", err)
}
if ParseConfig.InputFile != "stdin" {
absPath, _ := filepath.Abs(ParseConfig.InputFile)
workingDir = filepath.Dir(absPath)
} else {
workingDir, _ = os.Getwd()
}
}
workingDir = ConvertLinuxPath(workingDir)
log.Printf("workingDir: %s", workingDir)

dirStack := []string{workingDir}
Expand All @@ -80,7 +79,7 @@ func Parse(buildLog []string) {
if make_enter_dir.MatchString(line) {
group := make_enter_dir.FindStringSubmatch(line)
if group != nil {
dirStack = append([]string{group[1]}, dirStack...)
dirStack = append([]string{ConvertLinuxPath(group[1])}, dirStack...)
workingDir = dirStack[0]
log.Printf("change workingDir: %s", workingDir)
}
Expand Down Expand Up @@ -122,6 +121,7 @@ func Parse(buildLog []string) {
if ParseConfig.FullPath {
compileFullPath = GetBinFullPath(arguments[0])
if compileFullPath != "" {
compileFullPath = ConvertLinuxPath(compileFullPath)
arguments[0] = compileFullPath
}
}
Expand Down
34 changes: 34 additions & 0 deletions internal/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package internal

import (
"os"
"os/exec"
"strings"
)

func FileExist(filename string) bool {
_, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return true
}

func GetBinFullPath(name string) string {
path, err := exec.LookPath(name)
if err != nil {
return ""
}
return path
}

func ConvertLinuxPath(path string) string {
linuxPath := strings.ReplaceAll(path, "\\", "/")

// Handle drive letter (e.g., "C:\path" -> "/c/path")
// if len(linuxPath) > 1 && linuxPath[1] == ':' {
// linuxPath = "/" + strings.ToLower(string(linuxPath[0])) + linuxPath[2:]
// }

return linuxPath
}

0 comments on commit d1e66a1

Please sign in to comment.