package main import ( "errors" "flag" "fmt" "os" "os/exec" "path/filepath" "git.milar.in/milarin/buildinfo" "git.milar.in/milarin/configfile" ) func Init() { flag.Parse() var err error if *ShowVersion { buildinfo.Print(buildinfo.Options{}) os.Exit(0) } if *FindConfigFile { if configFilePath, err := configfile.Path("json"); err == nil { fmt.Println(configFilePath) os.Exit(0) } } if *SaveConfigFile { if configFilePath, err := SaveConfig(); err == nil { Println(ColorDone.Sprintf("config file saved at '%s'", configFilePath)) os.Exit(0) } else { Println(ColorError.Sprint(fmt.Errorf("saving config file failed: %w", err))) os.Exit(1) } } else if !*IgnoreConfigFile { if err := LoadConfig(); err != nil { Println(ColorError.Sprint(fmt.Errorf("loading config file failed: %w", err))) } } OutputFileTmpl.Parse(*OutputFile) ModulePath, err = filepath.Abs(flag.Arg(0)) if err != nil { Println(ColorError.Sprint("determining module path failed")) os.Exit(1) } *OutputDir, err = filepath.Abs(*OutputDir) if err != nil { Println(ColorError.Sprint("determining output path failed")) os.Exit(1) } if _, err := exec.LookPath("go"); err != nil { Println(ColorError.Sprint("go not found in PATH. compilation not possible")) os.Exit(1) } if err := FillCompileConfigs(); err != nil { Println(ColorError.Sprint(fmt.Errorf("target architectures could not be determined: %w", err))) os.Exit(1) } if _, err := os.Stat(filepath.Join(ModulePath, "go.mod")); errors.Is(err, os.ErrNotExist) { Println(ColorError.Sprintf("no Go module found at '%s'", ModulePath)) os.Exit(1) } if err := DetermineProjectName(); err != nil { Println(ColorError.Sprint(fmt.Errorf("project name could not be determined: %w", err))) os.Exit(1) } if err := os.MkdirAll(*OutputDir, 0744); err != nil { Println(ColorError.Sprint(fmt.Errorf("output folder '%s' could not be made: %w", *OutputDir, err))) os.Exit(1) } if err := CheckDirWritable(*OutputDir); err != nil { Println(ColorError.Sprintf("output folder '%s' has insufficient permissions", *OutputDir)) os.Exit(1) } if !*NoCompress { if _, err := exec.LookPath("upx"); err != nil { Println(ColorWarn.Sprint("upx not found in PATH. file compression not possible")) *NoCompress = true } } }