-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.go
106 lines (88 loc) · 3.18 KB
/
command.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package init_cfg
import (
"bufio"
"fmt"
"log"
"os"
"strings"
typeutil "github.com/cardinalby/xgo-pack/pkg/util/type"
yaml "github.com/goccy/go-yaml"
"github.com/spf13/cobra"
"github.com/cardinalby/xgo-pack/pkg/pipeline/config"
"github.com/cardinalby/xgo-pack/pkg/pipeline/config/cfgtypes"
fsutil "github.com/cardinalby/xgo-pack/pkg/util/fs"
)
func GetCommand(defaultCfgRelPath string) *cobra.Command {
return &cobra.Command{
Use: "init",
Short: "Initialise config file",
RunE: func(cmd *cobra.Command, args []string) error {
return initialiseConfig(defaultCfgRelPath)
},
}
}
func initialiseConfig(defaultCfgRelPath string) error {
// check if exists
_, err := os.Stat(defaultCfgRelPath)
if err == nil {
if !askForConfirmation(fmt.Sprintf("Config file '%s' already exists. Overwrite?", defaultCfgRelPath)) {
return nil
}
} else if !os.IsNotExist(err) {
return fmt.Errorf("error checking if config file '%s' exists: %w", defaultCfgRelPath, err)
}
var cfg cfgtypes.Config
if err := config.FillDefaults(&cfg); err != nil {
return err
}
cfg, commentMap := modifyInitCfg(cfg)
data, err := yaml.MarshalWithOptions(cfg, yaml.WithComment(commentMap))
if err != nil {
return fmt.Errorf("error marshaling config: %w", err)
}
if err := fsutil.WriteFile(defaultCfgRelPath, data); err != nil {
return fmt.Errorf("error writing config file '%s': %w", defaultCfgRelPath, err)
}
fmt.Printf("Default config was saved to %s", defaultCfgRelPath)
return nil
}
func modifyInitCfg(cfg cfgtypes.Config) (cfgtypes.Config, yaml.CommentMap) {
comments := make(yaml.CommentMap)
cfg.Root = ""
comments["$.src.icon"] = []*yaml.Comment{yaml.LineComment(" Set path to your icon")}
cfg.Targets.Windows.Amd64.BuildBin = typeutil.Ptr(true)
comments["$.targets.windows.amd64.build_bin"] = []*yaml.Comment{yaml.LineComment(" Build and keep binary")}
for arch, archCfg := range cfg.Targets.Linux.GetLinuxArches() {
yamlPath := fmt.Sprintf("$.targets.linux.%s", arch)
archCfg.BuildBin = typeutil.Ptr(true)
comments[yamlPath+".build_bin"] = []*yaml.Comment{yaml.LineComment(" Build and keep binary")}
archCfg.BuildDeb = typeutil.Ptr(true)
comments[yamlPath+".build_deb"] = []*yaml.Comment{yaml.LineComment(" Build and keep deb package")}
}
for arch, archCfg := range cfg.Targets.Macos.GetMacosArches() {
yamlPath := fmt.Sprintf("$.targets.macos.%s", arch)
archCfg.BuildBin = typeutil.Ptr(true)
comments[yamlPath+".build_bin"] = []*yaml.Comment{yaml.LineComment(" Build and keep binary")}
archCfg.BuildBundle = typeutil.Ptr(true)
comments[yamlPath+".build_bundle"] = []*yaml.Comment{yaml.LineComment(" Build and keep app bundle")}
archCfg.BuildDmg = typeutil.Ptr(true)
comments[yamlPath+".build_dmg"] = []*yaml.Comment{yaml.LineComment(" Build and keep dmg image with bundle")}
}
return cfg, comments
}
func askForConfirmation(s string) bool {
reader := bufio.NewReader(os.Stdin)
for {
fmt.Printf("%s [y/n]: ", s)
response, err := reader.ReadString('\n')
if err != nil {
log.Fatal(err)
}
response = strings.ToLower(strings.TrimSpace(response))
if response == "y" || response == "yes" {
return true
} else if response == "n" || response == "no" {
return false
}
}
}