-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[installer]: create config command and deprecate init command #12679
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ installer | |
dist | ||
versions.yaml | ||
!cmd/testdata/render/versions.yaml | ||
gitpod.config.yaml |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Copyright (c) 2022 Gitpod GmbH. All rights reserved. | ||
// Licensed under the MIT License. See License-MIT.txt in the project root for license information. | ||
|
||
package cmd | ||
|
||
import ( | ||
"errors" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/gitpod-io/gitpod/common-go/log" | ||
"github.com/gitpod-io/gitpod/installer/pkg/config" | ||
"github.com/spf13/cobra" | ||
"k8s.io/utils/pointer" | ||
) | ||
|
||
var configOpts struct { | ||
ConfigFile string | ||
} | ||
|
||
// configCmd represents the validate command | ||
var configCmd = &cobra.Command{ | ||
Use: "config", | ||
Short: "Perform configuration tasks", | ||
} | ||
|
||
// configFileExists checks if the configuration file is present on disk | ||
func configFileExists() (*bool, error) { | ||
if _, err := os.Stat(configOpts.ConfigFile); err == nil { | ||
return pointer.Bool(true), nil | ||
} else if errors.Is(err, os.ErrNotExist) { | ||
return pointer.Bool(false), nil | ||
} else { | ||
return nil, err | ||
} | ||
} | ||
|
||
// saveConfigFile converts the config to YAML and saves to disk | ||
func saveConfigFile(cfg interface{}) error { | ||
fc, err := config.Marshal(config.CurrentVersion, cfg) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = os.WriteFile(configOpts.ConfigFile, fc, 0644) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
log.Infof("File written to %s", configOpts.ConfigFile) | ||
|
||
return nil | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(configCmd) | ||
|
||
dir, err := os.Getwd() | ||
if err != nil { | ||
log.WithError(err).Fatal("Failed to get working directory") | ||
} | ||
|
||
configCmd.PersistentFlags().StringVarP(&configOpts.ConfigFile, "config", "c", getEnvvar("CONFIG_FILE", filepath.Join(dir, "gitpod.config.yaml")), "path to the configuration file") | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright (c) 2022 Gitpod GmbH. All rights reserved. | ||
// Licensed under the MIT License. See License-MIT.txt in the project root for license information. | ||
|
||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/gitpod-io/gitpod/installer/pkg/config" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var configInitOpts struct { | ||
OverwriteConfig bool | ||
} | ||
|
||
// configInitCmd represents the validate command | ||
var configInitCmd = &cobra.Command{ | ||
Use: "init", | ||
Short: "Create a base config file", | ||
Long: `Create a base config file | ||
|
||
This file contains all the credentials to install a Gitpod instance and | ||
be saved to a repository.`, | ||
Example: ` # Save config to config.yaml. | ||
gitpod-installer config init -c ./gitpod.config.yaml`, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
// Check file isn't present | ||
exists, err := configFileExists() | ||
if err != nil { | ||
return err | ||
} | ||
if *exists && !configInitOpts.OverwriteConfig { | ||
return fmt.Errorf("file %s exists - to overwrite add --overwrite flag", configOpts.ConfigFile) | ||
} | ||
|
||
cfg, err := config.NewDefaultConfig() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return saveConfigFile(cfg) | ||
}, | ||
} | ||
|
||
func init() { | ||
configCmd.AddCommand(configInitCmd) | ||
|
||
configInitCmd.Flags().BoolVar(&configInitOpts.OverwriteConfig, "overwrite", false, "overwrite config file if it exists") | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.