|
| 1 | +package porter |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "path/filepath" |
| 7 | + |
| 8 | + "get.porter.sh/porter/pkg" |
| 9 | + "get.porter.sh/porter/pkg/editor" |
| 10 | + "get.porter.sh/porter/pkg/tracing" |
| 11 | +) |
| 12 | + |
| 13 | +// ConfigShowOptions are the options for the ConfigShow command. |
| 14 | +type ConfigShowOptions struct{} |
| 15 | + |
| 16 | +// ConfigEditOptions are the options for the ConfigEdit command. |
| 17 | +type ConfigEditOptions struct{} |
| 18 | + |
| 19 | +// defaultConfigTemplate is the default TOML template for a new config file. |
| 20 | +const defaultConfigTemplate = `# Porter configuration |
| 21 | +# https://porter.sh/configuration/ |
| 22 | +
|
| 23 | +# verbosity = "info" |
| 24 | +# namespace = "" |
| 25 | +# default-storage-plugin = "mongodb-docker" |
| 26 | +# default-secrets-plugin = "host" |
| 27 | +` |
| 28 | + |
| 29 | +// GetConfigFilePath returns the path to the porter config file. |
| 30 | +// It checks for config files with extensions: toml, yaml, yml, json, hcl. |
| 31 | +// Returns the path, whether the file exists, and any error. |
| 32 | +func (p *Porter) GetConfigFilePath() (string, bool, error) { |
| 33 | + home, err := p.GetHomeDir() |
| 34 | + if err != nil { |
| 35 | + return "", false, fmt.Errorf("could not get porter home directory: %w", err) |
| 36 | + } |
| 37 | + |
| 38 | + extensions := []string{"toml", "yaml", "yml", "json", "hcl"} |
| 39 | + for _, ext := range extensions { |
| 40 | + path := filepath.Join(home, "config."+ext) |
| 41 | + exists, err := p.FileSystem.Exists(path) |
| 42 | + if err != nil { |
| 43 | + return "", false, fmt.Errorf("could not check if config file exists: %w", err) |
| 44 | + } |
| 45 | + if exists { |
| 46 | + return path, true, nil |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + // Default to toml if no config file exists |
| 51 | + return filepath.Join(home, "config.toml"), false, nil |
| 52 | +} |
| 53 | + |
| 54 | +// ConfigShow displays the contents of the porter config file. |
| 55 | +func (p *Porter) ConfigShow(ctx context.Context, opts ConfigShowOptions) error { |
| 56 | + _, span := tracing.StartSpan(ctx) |
| 57 | + defer span.EndSpan() |
| 58 | + |
| 59 | + path, exists, err := p.GetConfigFilePath() |
| 60 | + if err != nil { |
| 61 | + return span.Error(err) |
| 62 | + } |
| 63 | + |
| 64 | + if !exists { |
| 65 | + fmt.Fprintln(p.Out, "No configuration file found.") |
| 66 | + fmt.Fprintln(p.Out, "Use 'porter config edit' to create one.") |
| 67 | + return nil |
| 68 | + } |
| 69 | + |
| 70 | + contents, err := p.FileSystem.ReadFile(path) |
| 71 | + if err != nil { |
| 72 | + return span.Error(fmt.Errorf("could not read config file %s: %w", path, err)) |
| 73 | + } |
| 74 | + |
| 75 | + fmt.Fprintln(p.Out, string(contents)) |
| 76 | + return nil |
| 77 | +} |
| 78 | + |
| 79 | +// ConfigEdit opens the porter config file in an editor. |
| 80 | +// If the file does not exist, it creates a default template first. |
| 81 | +func (p *Porter) ConfigEdit(ctx context.Context, opts ConfigEditOptions) error { |
| 82 | + ctx, span := tracing.StartSpan(ctx) |
| 83 | + defer span.EndSpan() |
| 84 | + |
| 85 | + path, exists, err := p.GetConfigFilePath() |
| 86 | + if err != nil { |
| 87 | + return span.Error(err) |
| 88 | + } |
| 89 | + |
| 90 | + var contents []byte |
| 91 | + if exists { |
| 92 | + contents, err = p.FileSystem.ReadFile(path) |
| 93 | + if err != nil { |
| 94 | + return span.Error(fmt.Errorf("could not read config file %s: %w", path, err)) |
| 95 | + } |
| 96 | + } else { |
| 97 | + contents = []byte(defaultConfigTemplate) |
| 98 | + } |
| 99 | + |
| 100 | + ed := editor.New(p.Context, "porter-config.toml", contents) |
| 101 | + output, err := ed.Run(ctx) |
| 102 | + if err != nil { |
| 103 | + return span.Error(fmt.Errorf("unable to open editor: %w", err)) |
| 104 | + } |
| 105 | + |
| 106 | + // Ensure the directory exists |
| 107 | + dir := filepath.Dir(path) |
| 108 | + if err := p.FileSystem.MkdirAll(dir, pkg.FileModeDirectory); err != nil { |
| 109 | + return span.Error(fmt.Errorf("could not create config directory %s: %w", dir, err)) |
| 110 | + } |
| 111 | + |
| 112 | + if err := p.FileSystem.WriteFile(path, output, pkg.FileModeWritable); err != nil { |
| 113 | + return span.Error(fmt.Errorf("could not write config file %s: %w", path, err)) |
| 114 | + } |
| 115 | + |
| 116 | + return nil |
| 117 | +} |
0 commit comments