talm
/
main.go
128 строк · 3.9 Кб
1//go:generate go run tools/generate_presets.go
2
3package main
4
5import (
6"context"
7"fmt"
8"io/ioutil"
9"os"
10"path/filepath"
11"strings"
12"time"
13
14"gopkg.in/yaml.v3"
15
16"github.com/aenix-io/talm/pkg/commands"
17"github.com/siderolabs/talos/cmd/talosctl/cmd/common"
18"github.com/siderolabs/talos/pkg/machinery/constants"
19"github.com/spf13/cobra"
20)
21
22var Version = "dev"
23
24// rootCmd represents the base command when called without any subcommands.
25var rootCmd = &cobra.Command{
26Use: "talm",
27Short: "Manage Talos the GitOps Way!",
28Long: ``,
29Version: Version,
30SilenceErrors: true,
31SilenceUsage: true,
32DisableAutoGenTag: true,
33}
34
35func main() {
36if err := Execute(); err != nil {
37os.Exit(1)
38}
39}
40
41func Execute() error {
42rootCmd.PersistentFlags().StringVar(
43&commands.GlobalArgs.Talosconfig,
44"talosconfig",
45"",
46fmt.Sprintf("The path to the Talos configuration file. Defaults to '%s' env variable if set, otherwise '%s' and '%s' in order.",
47constants.TalosConfigEnvVar,
48filepath.Join("$HOME", constants.TalosDir, constants.TalosconfigFilename),
49filepath.Join(constants.ServiceAccountMountPath, constants.TalosconfigFilename),
50),
51)
52rootCmd.PersistentFlags().StringVar(&commands.Config.RootDir, "root", ".", "root directory of the project")
53rootCmd.PersistentFlags().StringVar(&commands.GlobalArgs.CmdContext, "context", "", "Context to be used in command")
54rootCmd.PersistentFlags().StringSliceVarP(&commands.GlobalArgs.Nodes, "nodes", "n", []string{}, "target the specified nodes")
55rootCmd.PersistentFlags().StringSliceVarP(&commands.GlobalArgs.Endpoints, "endpoints", "e", []string{}, "override default endpoints in Talos configuration")
56rootCmd.PersistentFlags().StringVar(&commands.GlobalArgs.Cluster, "cluster", "", "Cluster to connect to if a proxy endpoint is used.")
57rootCmd.PersistentFlags().Bool("version", false, "Print the version number of the application")
58
59cmd, err := rootCmd.ExecuteContextC(context.Background())
60if err != nil && !common.SuppressErrors {
61fmt.Fprintln(os.Stderr, err.Error())
62
63errorString := err.Error()
64// TODO: this is a nightmare, but arg-flag related validation returns simple `fmt.Errorf`, no way to distinguish
65// these errors
66if strings.Contains(errorString, "arg(s)") || strings.Contains(errorString, "flag") || strings.Contains(errorString, "command") {
67fmt.Fprintln(os.Stderr)
68fmt.Fprintln(os.Stderr, cmd.UsageString())
69}
70}
71
72return err
73}
74
75func init() {
76cobra.OnInitialize(initConfig)
77
78for _, cmd := range commands.Commands {
79rootCmd.AddCommand(cmd)
80}
81}
82
83func initConfig() {
84cmd, _, _ := rootCmd.Find(os.Args[1:])
85if cmd == nil {
86return
87}
88if strings.HasPrefix(cmd.Use, "init") {
89if strings.HasPrefix(Version, "v") {
90commands.Config.InitOptions.Version = strings.TrimPrefix(Version, `v`)
91} else {
92commands.Config.InitOptions.Version = "0.1.0"
93}
94} else {
95configFile := filepath.Join(commands.Config.RootDir, "Chart.yaml")
96if err := loadConfig(configFile); err != nil {
97fmt.Fprintf(os.Stderr, "Error loading configuration: %v\n", err)
98os.Exit(1)
99}
100}
101}
102
103func loadConfig(filename string) error {
104data, err := ioutil.ReadFile(filename)
105if err != nil {
106return fmt.Errorf("error reading configuration file: %w", err)
107}
108
109if err := yaml.Unmarshal(data, &commands.Config); err != nil {
110return fmt.Errorf("error unmarshalling configuration: %w", err)
111}
112if commands.GlobalArgs.Talosconfig == "" {
113commands.GlobalArgs.Talosconfig = commands.Config.GlobalOptions.Talosconfig
114}
115if commands.Config.TemplateOptions.KubernetesVersion == "" {
116commands.Config.TemplateOptions.KubernetesVersion = constants.DefaultKubernetesVersion
117}
118if commands.Config.ApplyOptions.Timeout == "" {
119commands.Config.ApplyOptions.Timeout = constants.ConfigTryTimeout.String()
120} else {
121var err error
122commands.Config.ApplyOptions.TimeoutDuration, err = time.ParseDuration(commands.Config.ApplyOptions.Timeout)
123if err != nil {
124panic(err)
125}
126}
127return nil
128}
129