-
Notifications
You must be signed in to change notification settings - Fork 0
feat(cli): add dotfiles command for managing configuration files #82
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
2 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 |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| package commands | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
|
|
||
| "github.com/malamtime/cli/model" | ||
| "github.com/sirupsen/logrus" | ||
| "github.com/urfave/cli/v2" | ||
| "go.opentelemetry.io/otel/attribute" | ||
| "go.opentelemetry.io/otel/trace" | ||
| ) | ||
|
|
||
| var DotfilesCommand *cli.Command = &cli.Command{ | ||
| Name: "dotfiles", | ||
| Usage: "manage dotfiles configuration", | ||
| Subcommands: []*cli.Command{ | ||
| { | ||
| Name: "push", | ||
| Usage: "push dotfiles to server", | ||
| Action: pushDotfiles, | ||
| Flags: []cli.Flag{ | ||
| &cli.StringSliceFlag{ | ||
| Name: "apps", | ||
| Aliases: []string{"a"}, | ||
| Usage: "specify which apps to push (nvim, fish, git, zsh, bash, ghostty). If empty, pushes all", | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| OnUsageError: func(cCtx *cli.Context, err error, isSubcommand bool) error { | ||
| return nil | ||
| }, | ||
| } | ||
|
|
||
| func pushDotfiles(c *cli.Context) error { | ||
| ctx, span := commandTracer.Start(c.Context, "dotfiles-push", trace.WithSpanKind(trace.SpanKindClient)) | ||
| defer span.End() | ||
| SetupLogger(os.ExpandEnv("$HOME/" + model.COMMAND_BASE_STORAGE_FOLDER)) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| logrus.SetLevel(logrus.TraceLevel) | ||
|
|
||
| apps := c.StringSlice("apps") | ||
| span.SetAttributes(attribute.StringSlice("apps", apps)) | ||
|
|
||
| config, err := configService.ReadConfigFile(ctx) | ||
| if err != nil { | ||
| logrus.Errorln(err) | ||
| return err | ||
| } | ||
|
|
||
| if config.Token == "" { | ||
| return fmt.Errorf("no token found, please run 'shelltime auth login' first") | ||
| } | ||
|
|
||
| mainEndpoint := model.Endpoint{ | ||
| APIEndpoint: config.APIEndpoint, | ||
| Token: config.Token, | ||
| } | ||
|
|
||
| // Initialize all available app handlers | ||
| allApps := []model.DotfileApp{ | ||
| model.NewNvimApp(), | ||
| model.NewFishApp(), | ||
| model.NewGitApp(), | ||
| model.NewZshApp(), | ||
| model.NewBashApp(), | ||
| model.NewGhosttyApp(), | ||
| } | ||
|
|
||
| // Filter apps based on user input | ||
| var selectedApps []model.DotfileApp | ||
| if len(apps) == 0 { | ||
| // If no apps specified, use all | ||
| selectedApps = allApps | ||
| } else { | ||
| // Filter based on user selection | ||
| appMap := make(map[string]model.DotfileApp) | ||
| for _, app := range allApps { | ||
| appMap[app.Name()] = app | ||
| } | ||
|
|
||
| for _, appName := range apps { | ||
| if app, ok := appMap[appName]; ok { | ||
| selectedApps = append(selectedApps, app) | ||
| } else { | ||
| logrus.Warnf("Unknown app: %s", appName) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Collect all dotfiles | ||
| var allDotfiles []model.DotfileItem | ||
| for _, app := range selectedApps { | ||
| logrus.Infof("Collecting dotfiles for %s", app.Name()) | ||
| dotfiles, err := app.CollectDotfiles(ctx) | ||
| if err != nil { | ||
| logrus.Errorf("Failed to collect dotfiles for %s: %v", app.Name(), err) | ||
| continue | ||
| } | ||
| allDotfiles = append(allDotfiles, dotfiles...) | ||
| } | ||
|
|
||
| if len(allDotfiles) == 0 { | ||
| logrus.Infoln("No dotfiles found to push") | ||
| return nil | ||
| } | ||
|
|
||
| // Send to server | ||
| logrus.Infof("Pushing %d dotfiles to server", len(allDotfiles)) | ||
| userID, err := model.SendDotfilesToServer(ctx, mainEndpoint, allDotfiles) | ||
| if err != nil { | ||
| logrus.Errorln("Failed to send dotfiles to server:", err) | ||
| return err | ||
| } | ||
|
|
||
| // Generate web link for managing dotfiles | ||
| webLink := fmt.Sprintf("%s/users/%d/settings/dotfiles", config.WebEndpoint, userID) | ||
| logrus.Infof("Successfully pushed dotfiles. Manage them at: %s", webLink) | ||
| fmt.Printf("\n✅ Successfully pushed %d dotfiles to server\n", len(allDotfiles)) | ||
| fmt.Printf("📁 Manage your dotfiles at: %s\n", webLink) | ||
|
|
||
| return nil | ||
| } | ||
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,144 @@ | ||
| package model | ||
|
|
||
| import ( | ||
| "context" | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestReadConfigFileWithLocal(t *testing.T) { | ||
| // Create a temporary directory for test configs | ||
| tmpDir, err := os.MkdirTemp("", "shelltime-test-*") | ||
| require.NoError(t, err) | ||
| defer os.RemoveAll(tmpDir) | ||
|
|
||
| // Create base config file | ||
| baseConfigPath := filepath.Join(tmpDir, "config.toml") | ||
| baseConfig := `Token = 'base-token' | ||
| APIEndpoint = 'https://api.base.com' | ||
| WebEndpoint = 'https://base.com' | ||
| FlushCount = 5 | ||
| GCTime = 7 | ||
| dataMasking = false | ||
| enableMetrics = false | ||
| encrypted = false` | ||
| err = os.WriteFile(baseConfigPath, []byte(baseConfig), 0644) | ||
| require.NoError(t, err) | ||
|
|
||
| // Create local config file that overrides some settings | ||
| localConfigPath := filepath.Join(tmpDir, "config.local.toml") | ||
| localConfig := `Token = 'local-token' | ||
| APIEndpoint = 'https://api.local.com' | ||
| FlushCount = 10 | ||
| dataMasking = true` | ||
| err = os.WriteFile(localConfigPath, []byte(localConfig), 0644) | ||
| require.NoError(t, err) | ||
|
|
||
| // Test reading config with local override | ||
| cs := NewConfigService(baseConfigPath) | ||
| config, err := cs.ReadConfigFile(context.Background()) | ||
| require.NoError(t, err) | ||
|
|
||
| // Verify local config overrides base config | ||
| assert.Equal(t, "local-token", config.Token, "Token should be overridden by local config") | ||
| assert.Equal(t, "https://api.local.com", config.APIEndpoint, "APIEndpoint should be overridden by local config") | ||
| assert.Equal(t, 10, config.FlushCount, "FlushCount should be overridden by local config") | ||
| assert.True(t, *config.DataMasking, "DataMasking should be overridden by local config") | ||
|
|
||
| // Verify base config values that weren't overridden | ||
| assert.Equal(t, "https://base.com", config.WebEndpoint, "WebEndpoint should keep base value") | ||
| assert.Equal(t, 7, config.GCTime, "GCTime should keep base value") | ||
| assert.False(t, *config.EnableMetrics, "EnableMetrics should keep base value") | ||
| assert.False(t, *config.Encrypted, "Encrypted should keep base value") | ||
| } | ||
|
|
||
| func TestReadConfigFileWithoutLocal(t *testing.T) { | ||
| // Create a temporary directory for test configs | ||
| tmpDir, err := os.MkdirTemp("", "shelltime-test-*") | ||
| require.NoError(t, err) | ||
| defer os.RemoveAll(tmpDir) | ||
|
|
||
| // Create only base config file (no local file) | ||
| baseConfigPath := filepath.Join(tmpDir, "config.toml") | ||
| baseConfig := `Token = 'base-token' | ||
| APIEndpoint = 'https://api.base.com' | ||
| WebEndpoint = 'https://base.com' | ||
| FlushCount = 5 | ||
| GCTime = 7` | ||
| err = os.WriteFile(baseConfigPath, []byte(baseConfig), 0644) | ||
| require.NoError(t, err) | ||
|
|
||
| // Test reading config without local file | ||
| cs := NewConfigService(baseConfigPath) | ||
| config, err := cs.ReadConfigFile(context.Background()) | ||
| require.NoError(t, err) | ||
|
|
||
| // Verify base config values are used | ||
| assert.Equal(t, "base-token", config.Token) | ||
| assert.Equal(t, "https://api.base.com", config.APIEndpoint) | ||
| assert.Equal(t, "https://base.com", config.WebEndpoint) | ||
| assert.Equal(t, 5, config.FlushCount) | ||
| assert.Equal(t, 7, config.GCTime) | ||
| } | ||
|
|
||
| func TestReadConfigFileWithDifferentExtensions(t *testing.T) { | ||
| testCases := []struct { | ||
| name string | ||
| configFile string | ||
| localFile string | ||
| }{ | ||
| { | ||
| name: "TOML files", | ||
| configFile: "config.toml", | ||
| localFile: "config.local.toml", | ||
| }, | ||
| { | ||
| name: "Custom config name", | ||
| configFile: "shelltime-config.toml", | ||
| localFile: "shelltime-config.local.toml", | ||
| }, | ||
| { | ||
| name: "Different extension", | ||
| configFile: "settings.conf", | ||
| localFile: "settings.local.conf", | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range testCases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| // Create a temporary directory for test configs | ||
| tmpDir, err := os.MkdirTemp("", "shelltime-test-*") | ||
| require.NoError(t, err) | ||
| defer os.RemoveAll(tmpDir) | ||
|
|
||
| // Create base config file | ||
| baseConfigPath := filepath.Join(tmpDir, tc.configFile) | ||
| baseConfig := `Token = 'base-token' | ||
| APIEndpoint = 'https://api.base.com' | ||
| FlushCount = 5` | ||
| err = os.WriteFile(baseConfigPath, []byte(baseConfig), 0644) | ||
| require.NoError(t, err) | ||
|
|
||
| // Create local config file | ||
| localConfigPath := filepath.Join(tmpDir, tc.localFile) | ||
| localConfig := `Token = 'local-token' | ||
| FlushCount = 10` | ||
| err = os.WriteFile(localConfigPath, []byte(localConfig), 0644) | ||
| require.NoError(t, err) | ||
|
|
||
| // Test reading config with local override | ||
| cs := NewConfigService(baseConfigPath) | ||
| config, err := cs.ReadConfigFile(context.Background()) | ||
| require.NoError(t, err) | ||
|
|
||
| // Verify local config overrides base config | ||
| assert.Equal(t, "local-token", config.Token, "Token should be overridden by local config for %s", tc.name) | ||
| assert.Equal(t, 10, config.FlushCount, "FlushCount should be overridden by local config for %s", tc.name) | ||
| assert.Equal(t, "https://api.base.com", config.APIEndpoint, "APIEndpoint should keep base value for %s", tc.name) | ||
| }) | ||
| } | ||
| } |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Swallowing usage errors by returning
nilprovides a poor user experience, as users won't be notified of incorrect command usage. It's better to remove thisOnUsageErrorhandler and let theurfave/cliframework provide its default, helpful error messages.