gocc/init.go

96 lines
2.3 KiB
Go
Raw Normal View History

2022-08-17 19:18:57 +02:00
package main
import (
"errors"
"flag"
"fmt"
"os"
"os/exec"
"path/filepath"
2022-08-29 10:42:18 +02:00
"git.milar.in/milarin/buildinfo"
"git.milar.in/milarin/configfile"
2022-08-17 19:18:57 +02:00
)
func Init() {
flag.Parse()
var err error
2022-08-17 23:55:15 +02:00
if *ShowVersion {
2022-08-29 10:42:18 +02:00
buildinfo.Print(buildinfo.Options{})
2022-08-17 23:55:15 +02:00
os.Exit(0)
}
if *FindConfigFile {
if configFilePath, err := configfile.Path("json"); err == nil {
2022-08-17 23:55:15 +02:00
fmt.Println(configFilePath)
os.Exit(0)
}
}
2022-08-17 20:37:12 +02:00
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 {
2022-08-17 20:37:12 +02:00
if err := LoadConfig(); err != nil {
Println(ColorError.Sprint(fmt.Errorf("loading config file failed: %w", err)))
}
}
OutputFileTmpl.Parse(*OutputFile)
2022-08-17 19:18:57 +02:00
ModulePath, err = filepath.Abs(flag.Arg(0))
if err != nil {
2022-08-17 20:37:12 +02:00
Println(ColorError.Sprint("determining module path failed"))
2022-08-17 19:18:57 +02:00
os.Exit(1)
}
*OutputDir, err = filepath.Abs(*OutputDir)
if err != nil {
2022-08-17 20:37:12 +02:00
Println(ColorError.Sprint("determining output path failed"))
2022-08-17 19:18:57 +02:00
os.Exit(1)
}
if _, err := exec.LookPath("go"); err != nil {
2022-08-17 20:37:12 +02:00
Println(ColorError.Sprint("go not found in PATH. compilation not possible"))
2022-08-17 19:18:57 +02:00
os.Exit(1)
}
if err := FillCompileConfigs(); err != nil {
2022-08-17 20:37:12 +02:00
Println(ColorError.Sprint(fmt.Errorf("target architectures could not be determined: %w", err)))
2022-08-17 19:18:57 +02:00
os.Exit(1)
}
if _, err := os.Stat(filepath.Join(ModulePath, "go.mod")); errors.Is(err, os.ErrNotExist) {
2022-08-17 20:37:12 +02:00
Println(ColorError.Sprintf("no Go module found at '%s'", ModulePath))
2022-08-17 19:18:57 +02:00
os.Exit(1)
}
if err := DetermineProjectName(); err != nil {
2022-08-17 20:37:12 +02:00
Println(ColorError.Sprint(fmt.Errorf("project name could not be determined: %w", err)))
2022-08-17 19:18:57 +02:00
os.Exit(1)
}
if err := os.MkdirAll(*OutputDir, 0744); err != nil {
2022-08-17 20:37:12 +02:00
Println(ColorError.Sprint(fmt.Errorf("output folder '%s' could not be made: %w", *OutputDir, err)))
2022-08-17 19:18:57 +02:00
os.Exit(1)
}
if err := CheckDirWritable(*OutputDir); err != nil {
2022-08-17 20:37:12 +02:00
Println(ColorError.Sprintf("output folder '%s' has insufficient permissions", *OutputDir))
2022-08-17 19:18:57 +02:00
os.Exit(1)
}
if !*NoCompress {
if _, err := exec.LookPath("upx"); err != nil {
2022-08-17 22:55:17 +02:00
Println(ColorWarn.Sprint("upx not found in PATH. file compression not possible"))
2022-08-17 19:18:57 +02:00
*NoCompress = true
}
}
}