gocc/init.go

90 lines
2.2 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"
"git.milar.in/milarin/configfile"
2022-08-17 19:18:57 +02:00
)
func Init() {
flag.Parse()
var err error
if *FindConfigFile {
if configFilePath, err := configfile.Path("json"); err == nil {
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
}
}
}