gocc/init.go
2022-08-17 19:18:57 +02:00

66 lines
1.6 KiB
Go

package main
import (
"errors"
"flag"
"fmt"
"os"
"os/exec"
"path/filepath"
)
func Init() {
flag.Parse()
var err error
ModulePath, err = filepath.Abs(flag.Arg(0))
if err != nil {
ColorError.Fprintln(os.Stderr, "determining module path failed")
os.Exit(1)
}
*OutputDir, err = filepath.Abs(*OutputDir)
if err != nil {
ColorError.Fprintln(os.Stderr, "determining output path failed")
os.Exit(1)
}
if _, err := exec.LookPath("go"); err != nil {
ColorError.Fprintln(os.Stderr, "go not found in PATH. compilation not possible")
os.Exit(1)
}
if err := FillCompileConfigs(); err != nil {
ColorError.Fprintln(os.Stderr, 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) {
ColorError.Fprintf(os.Stderr, "no Go module found at '%s'\n", ModulePath)
os.Exit(1)
}
if err := DetermineProjectName(); err != nil {
ColorError.Fprintln(os.Stderr, fmt.Errorf("project name could not be determined: %w", err))
os.Exit(1)
}
if err := os.MkdirAll(*OutputDir, 0744); err != nil {
ColorError.Fprintln(os.Stderr, fmt.Errorf("output folder '%s' could not be made: %w", *OutputDir, err))
os.Exit(1)
}
if err := CheckDirWritable(*OutputDir); err != nil {
ColorError.Fprintf(os.Stderr, "output folder '%s' has insufficient permissions\n", *OutputDir)
os.Exit(1)
}
if !*NoCompress {
if _, err := exec.LookPath("upx"); err != nil {
ColorWarn.Fprintln(os.Stderr, "upx not found in PATH. file compression not possible")
*NoCompress = true
}
}
}