config file introduced

This commit is contained in:
Timon Ringwald 2022-08-17 20:37:12 +02:00
parent cd24640118
commit 5caf8fe9fa
6 changed files with 151 additions and 43 deletions

View File

@ -1,8 +1,82 @@
package main
import (
"encoding/json"
"os"
"path/filepath"
"strings"
"git.milar.in/milarin/configfile"
)
type Config struct {
OS []string `json:"os"`
Arch []string `json:"arch"`
NoCompress bool `json:"no_compress"`
NumThreads int `json:"num_threads"`
OutputDir string `json:"output_dir"`
OS []string `json:"os"`
Arch []string `json:"arch"`
Silent bool `json:"silent"`
NoCompress bool `json:"no_compress"`
NumThreads int `json:"num_threads"`
}
func LoadConfig() error {
configFilePath, err := configfile.Path("json")
if err != nil {
return err
}
file, err := os.Open(configFilePath)
if err != nil {
return err
}
defer file.Close()
cfg := &Config{}
if err := json.NewDecoder(file).Decode(cfg); err != nil {
return err
}
*OutputDir = cfg.OutputDir
*OS = strings.Join(cfg.OS, ",")
*Arch = strings.Join(cfg.Arch, ",")
*Silent = cfg.Silent
*NoCompress = cfg.NoCompress
*NumThreads = cfg.NumThreads
return nil
}
func SaveConfig() (string, error) {
configFilePath, err := configfile.Path("json")
if err != nil {
return "", err
}
if err := os.MkdirAll(filepath.Dir(configFilePath), 0744); err != nil {
return configFilePath, err
}
file, err := os.Create(configFilePath)
if err != nil {
return configFilePath, err
}
defer file.Close()
enc := json.NewEncoder(file)
enc.SetIndent("", "\t")
cfg := &Config{
OutputDir: *OutputDir,
OS: strings.Split(*OS, ","),
Arch: strings.Split(*Arch, ","),
Silent: *Silent,
NoCompress: *NoCompress,
NumThreads: *NumThreads,
}
if err := enc.Encode(cfg); err != nil {
return configFilePath, err
}
return configFilePath, nil
}

7
go.mod
View File

@ -4,13 +4,14 @@ go 1.19
require (
git.milar.in/milarin/channel v0.0.7
git.milar.in/milarin/configfile v1.0.2
git.milar.in/milarin/gmath v0.0.1
github.com/fatih/color v1.13.0
golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e
)
require (
github.com/mattn/go-colorable v0.1.9 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
golang.org/x/sys v0.0.0-20211019181941-9d821ace8654 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.16 // indirect
golang.org/x/sys v0.0.0-20220817070843-5a390386f1f2 // indirect
)

13
go.sum
View File

@ -1,18 +1,23 @@
git.milar.in/milarin/channel v0.0.7 h1:cVKtwgH/EE7U+XTHcoFCClJ4LR349KanzjX9xKwRcNg=
git.milar.in/milarin/channel v0.0.7/go.mod h1:We83LTI8S7u7II3pD+A2ChCDWJfCkcBUCUqii9HjTtM=
git.milar.in/milarin/configfile v1.0.2 h1:QgZVSVDsFm3HK7PEg6a2ANeZxqo0JlIooyyJv8VaCNI=
git.milar.in/milarin/configfile v1.0.2/go.mod h1:RsdN0d/a13WWJmxhuPjdexfUfLCeXAzBenbqz+KMkTs=
git.milar.in/milarin/gmath v0.0.1 h1:pjUc1vYUE4bIky1fk0y0K347wU/9F9KtW312xkTF1YA=
git.milar.in/milarin/gmath v0.0.1/go.mod h1:HDLftG5RLpiNGKiIWh+O2G1PYkNzyLDADO8Cd/1abiE=
github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w=
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
github.com/mattn/go-colorable v0.1.9 h1:sqDoxXbdeALODt0DAeJCVp38ps9ZogZEAXjus69YV3U=
github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peKQ=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e h1:+WEEuIdZHnUeJJmEUjyYC2gfUMj69yZXw17EnHg/otA=
golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e/go.mod h1:Kr81I6Kryrl9sr8s2FK3vxD90NdsKWRuOIl2O4CvYbA=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211019181941-9d821ace8654 h1:id054HUawV2/6IGm2IV8KZQjqtwAOo2CYlOToYqa0d0=
golang.org/x/sys v0.0.0-20211019181941-9d821ace8654/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220817070843-5a390386f1f2 h1:fqTvyMIIj+HRzMmnzr9NtpHP6uVpvB5fkHcgPDC4nu8=
golang.org/x/sys v0.0.0-20220817070843-5a390386f1f2/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

49
init.go
View File

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

25
main.go
View File

@ -24,15 +24,28 @@ var (
MaxConfigStringLength int
)
// command line arguments default values
var (
DefaultOutputDir = "output"
DefaultOS = ""
DefaultArch = ""
DefaultSilent = false
DefaultNoCompress = false
DefaultNumThreads = runtime.NumCPU()
)
// command line arguments
var (
OS = flag.String("os", "", "comma-separated list of operating systems to compile for (empty for all)")
Arch = flag.String("arch", "", "comma-separated list of architectures to compile for (empty for all)")
OutputDir = flag.String("o", DefaultOutputDir, "output directory")
OutputDir = flag.String("o", "output", "output directory")
Silent = flag.Bool("s", false, "silent mode (no output)")
NoCompress = flag.Bool("c", false, "dont compress any executables")
NumThreads = flag.Int("t", runtime.NumCPU(), "amount of threads (0 = infinite)")
OS = flag.String("os", DefaultOS, "comma-separated list of operating systems to compile for (empty for all)")
Arch = flag.String("arch", DefaultArch, "comma-separated list of architectures to compile for (empty for all)")
Silent = flag.Bool("s", DefaultSilent, "silent mode (no output)")
NoCompress = flag.Bool("c", DefaultNoCompress, "dont compress any executables")
NumThreads = flag.Int("t", DefaultNumThreads, "amount of threads (0 = infinite)")
SaveConfigFile = flag.Bool("saveconfig", false, "save config file with current configuration and exit")
)
func main() {

18
print.go Normal file
View File

@ -0,0 +1,18 @@
package main
import (
"fmt"
"os"
)
func Println(args ...interface{}) {
if !*Silent {
fmt.Fprintln(os.Stderr, args...)
}
}
func Printf(format string, args ...interface{}) {
if !*Silent {
fmt.Fprintf(os.Stderr, format, args...)
}
}