generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add "ftl profile ..." command tree (#2862)
Also refactor profiles package a bit to be more flexible. ``` 🐚 ~/dev/ftl $ ftl profile init ftl Project initialized in /Users/alec/dev/ftl. 🐚 ~/dev/ftl $ ftl profile new --local test 🐚 ~/dev/ftl $ ftl profile list local (local, default) test (local) 🐚 ~/dev/ftl $ ftl profile switch test 🐚 ~/dev/ftl $ ftl profile list local (local, default) test (local, active) ``` --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
- Loading branch information
1 parent
6bd2cc9
commit 8888126
Showing
7 changed files
with
458 additions
and
65 deletions.
There are no files selected for viewing
This file contains 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 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 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,133 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"net/url" | ||
"strings" | ||
|
||
"github.com/alecthomas/types/either" | ||
|
||
"github.com/TBD54566975/ftl" | ||
"github.com/TBD54566975/ftl/internal/configuration" | ||
"github.com/TBD54566975/ftl/internal/configuration/providers" | ||
"github.com/TBD54566975/ftl/internal/profiles" | ||
) | ||
|
||
type profileCmd struct { | ||
Init profileInitCmd `cmd:"" help:"Initialize a new project."` | ||
List profileListCmd `cmd:"" help:"List all profiles."` | ||
Default profileDefaultCmd `cmd:"" help:"Set a profile as default."` | ||
Switch profileSwitchCmd `cmd:"" help:"Switch locally active profile."` | ||
New profileNewCmd `cmd:"" help:"Create a new profile."` | ||
} | ||
|
||
type profileInitCmd struct { | ||
Project string `arg:"" help:"Name of the project."` | ||
Dir string `arg:"" help:"Directory to initialize the project in." default:"${gitroot}" required:""` | ||
ModuleRoots []string `help:"Root directories of existing modules."` | ||
NoGit bool `help:"Don't add files to the git repository."` | ||
} | ||
|
||
func (p profileInitCmd) Run( | ||
configRegistry *providers.Registry[configuration.Configuration], | ||
secretsRegistry *providers.Registry[configuration.Secrets], | ||
) error { | ||
_, err := profiles.Init(profiles.ProjectConfig{ | ||
Realm: p.Project, | ||
FTLMinVersion: ftl.Version, | ||
ModuleRoots: p.ModuleRoots, | ||
NoGit: p.NoGit, | ||
Root: p.Dir, | ||
}, secretsRegistry, configRegistry) | ||
if err != nil { | ||
return fmt.Errorf("init project: %w", err) | ||
} | ||
fmt.Printf("Project initialized in %s.\n", p.Dir) | ||
return nil | ||
} | ||
|
||
type profileListCmd struct{} | ||
|
||
func (profileListCmd) Run(project *profiles.Project) error { | ||
active, err := project.ActiveProfile() | ||
if err != nil { | ||
return fmt.Errorf("active profile: %w", err) | ||
} | ||
p, err := project.List() | ||
if err != nil { | ||
return fmt.Errorf("list profiles: %w", err) | ||
} | ||
for _, profile := range p { | ||
attrs := []string{} | ||
switch profile.Config.(type) { | ||
case either.Left[profiles.LocalProfileConfig, profiles.RemoteProfileConfig]: | ||
attrs = append(attrs, "local") | ||
case either.Right[profiles.LocalProfileConfig, profiles.RemoteProfileConfig]: | ||
attrs = append(attrs, "remote") | ||
} | ||
if project.DefaultProfile() == profile.Name { | ||
attrs = append(attrs, "default") | ||
} | ||
if active == profile.Name { | ||
attrs = append(attrs, "active") | ||
} | ||
fmt.Printf("%s (%s)\n", profile, strings.Join(attrs, ", ")) | ||
} | ||
return nil | ||
} | ||
|
||
type profileDefaultCmd struct { | ||
Profile string `arg:"" help:"Profile name."` | ||
} | ||
|
||
func (p profileDefaultCmd) Run(project *profiles.Project) error { | ||
err := project.SetDefault(p.Profile) | ||
if err != nil { | ||
return fmt.Errorf("set default profile: %w", err) | ||
} | ||
return nil | ||
} | ||
|
||
type profileSwitchCmd struct { | ||
Profile string `arg:"" help:"Profile name."` | ||
} | ||
|
||
func (p profileSwitchCmd) Run(project *profiles.Project) error { | ||
err := project.Switch(p.Profile) | ||
if err != nil { | ||
return fmt.Errorf("switch profile: %w", err) | ||
} | ||
return nil | ||
} | ||
|
||
type profileNewCmd struct { | ||
Local bool `help:"Create a local profile." xor:"location" and:"providers"` | ||
Remote *url.URL `help:"Create a remote profile." xor:"location"` | ||
Secrets configuration.ProviderKey `help:"Secrets provider." placeholder:"PROVIDER" default:"inline" and:"providers"` | ||
Configuration configuration.ProviderKey `help:"Configuration provider." placeholder:"PROVIDER" default:"inline" and:"providers"` | ||
Name string `arg:"" help:"Profile name."` | ||
} | ||
|
||
func (p profileNewCmd) Run(project *profiles.Project) error { | ||
var config either.Either[profiles.LocalProfileConfig, profiles.RemoteProfileConfig] | ||
switch { | ||
case p.Local: | ||
config = either.LeftOf[profiles.RemoteProfileConfig](profiles.LocalProfileConfig{ | ||
SecretsProvider: p.Secrets, | ||
ConfigProvider: p.Configuration, | ||
}) | ||
|
||
case p.Remote != nil: | ||
config = either.RightOf[profiles.LocalProfileConfig](profiles.RemoteProfileConfig{ | ||
Endpoint: p.Remote, | ||
}) | ||
} | ||
err := project.New(profiles.ProfileConfig{ | ||
Name: p.Name, | ||
Config: config, | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("new profile: %w", err) | ||
} | ||
return nil | ||
} |
This file contains 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
Oops, something went wrong.