Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/install/installer/cmd/config.go
2498 views
1
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
2
/// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package cmd
6
7
import (
8
"errors"
9
"fmt"
10
"os"
11
"path/filepath"
12
13
"github.com/gitpod-io/gitpod/common-go/log"
14
"github.com/gitpod-io/gitpod/installer/pkg/config"
15
"github.com/spf13/cobra"
16
"k8s.io/utils/pointer"
17
)
18
19
var configOpts struct {
20
ConfigFile string
21
}
22
23
// configCmd represents the validate command
24
var configCmd = &cobra.Command{
25
Use: "config",
26
Short: "Perform configuration tasks",
27
}
28
29
// configFileExists checks if the configuration file is present on disk
30
func configFileExists() (*bool, error) {
31
if _, err := os.Stat(configOpts.ConfigFile); err == nil {
32
return pointer.Bool(true), nil
33
} else if errors.Is(err, os.ErrNotExist) {
34
return pointer.Bool(false), nil
35
} else {
36
return nil, err
37
}
38
}
39
40
// configFileExistsAndInit checks if the config file exists, if not it returns a "run config init" error
41
func configFileExistsAndInit() (*bool, error) {
42
// Check file is present
43
exists, err := configFileExists()
44
if err != nil {
45
return nil, err
46
}
47
if !*exists {
48
return nil, fmt.Errorf(`file %s does not exist - please run "config init"`, configOpts.ConfigFile)
49
}
50
return exists, nil
51
}
52
53
// saveConfigFile converts the config to YAML and saves to disk
54
func saveConfigFile(cfg interface{}) error {
55
fc, err := config.Marshal(config.CurrentVersion, cfg)
56
if err != nil {
57
return err
58
}
59
60
err = os.WriteFile(configOpts.ConfigFile, fc, 0644)
61
if err != nil {
62
return err
63
}
64
65
log.Infof("File written to %s", configOpts.ConfigFile)
66
67
return nil
68
}
69
70
func init() {
71
rootCmd.AddCommand(configCmd)
72
73
dir, err := os.Getwd()
74
if err != nil {
75
log.WithError(err).Fatal("Failed to get working directory")
76
}
77
78
configCmd.PersistentFlags().StringVarP(&configOpts.ConfigFile, "config", "c", getEnvvar("GITPOD_INSTALLER_CONFIG", filepath.Join(dir, "gitpod.config.yaml")), "path to the configuration file")
79
}
80
81