forked from dotzero/git-profile
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from Dm3Ch/store_profiles_as_separate_configs
Store profiles as separate configs
- Loading branch information
Showing
27 changed files
with
2,055 additions
and
556 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
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 |
---|---|---|
@@ -1,36 +1,63 @@ | ||
package cmd | ||
|
||
import ( | ||
"log" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/dm3ch/git-profile-manager/config" | ||
"github.com/dm3ch/git-profile-manager/profile" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
var addCmd = &cobra.Command{ //nolint:gochecknoglobals | ||
Use: "add [profile] [key] [value]", | ||
Aliases: []string{"set"}, | ||
Short: "Add an entry to a profile", | ||
Long: "Adds an entry to a profile or update exists profile.", | ||
Example: ` git-profile add my-profile user.email work@example.com | ||
git-profile add my-profile user.name "John Doe" | ||
git-profile add my-profile user.signingkey AAAAAAAA`, | ||
Args: cobra.ExactArgs(3), | ||
Run: addRun, | ||
} | ||
var addCmd = &cobra.Command{ | ||
Use: "add [profile name]", | ||
Short: "Add git profile", | ||
Args: cobra.ExactArgs(1), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
profile := new(profile.Profile) | ||
profile.Name = args[0] | ||
profile.User.Name, _ = cmd.Flags().GetString("name") | ||
profile.User.Email, _ = cmd.Flags().GetString("email") | ||
profile.User.SigningKey, _ = cmd.Flags().GetString("signingkey") | ||
force, _ := cmd.Flags().GetBool("force") | ||
|
||
configDir, err := getConfigDirAbsolutePath() | ||
if err != nil { | ||
fmt.Println("Can't get configuration directory absolute path:") | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
|
||
func addRun(cmd *cobra.Command, args []string) { | ||
profile := args[0] | ||
key := args[1] | ||
value := args[2] | ||
if profile.User.Name == "" && profile.User.Email == "" && profile.User.SigningKey == "" { | ||
promptGitUser(&profile.User) | ||
} | ||
|
||
cfgStorage.SetValue(profile, config.Entry{Key: key, Value: value}) | ||
err := cfgStorage.Save(cfgFile) | ||
if err != nil { | ||
log.Println("[ERROR] Cannot save json to", cfgFile, err) | ||
os.Exit(1) | ||
} | ||
path := getProfilePath(configDir, profile.Name) | ||
profileExists := isFileExist(path) | ||
|
||
if profileExists && !force { | ||
force = promptYesNo(fmt.Sprintf("Override existing %s profile", profile.Name)) | ||
} | ||
|
||
if !profileExists || force { | ||
err = profile.Save(path) | ||
if err != nil { | ||
fmt.Printf("Profile %s save failed:\n", profile.Name) | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} else { | ||
fmt.Printf("Profile %s added successfully\n", profile.Name) | ||
} | ||
} else { | ||
fmt.Printf("Profile %s wasn't added\n", profile.Name) | ||
} | ||
}, | ||
} | ||
|
||
cmd.Printf("Successfully added `%s=%s` to `%s` profile.", key, value, profile) | ||
func init() { | ||
rootCmd.AddCommand(addCmd) | ||
addCmd.Flags().StringP("name", "n", "", "Git user.name") | ||
addCmd.Flags().StringP("email", "e", "", "Git user.email") | ||
addCmd.Flags().StringP("signingkey", "s", "", "Git user.signingkey") | ||
addCmd.Flags().BoolP("force", "f", false, "Override exitsting profile") | ||
} |
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 |
---|---|---|
@@ -1,33 +1,78 @@ | ||
package cmd | ||
|
||
import ( | ||
"os" | ||
"fmt" | ||
|
||
"github.com/dm3ch/git-profile-manager/git" | ||
"github.com/dm3ch/git-profile-manager/gitconfig" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// DefaultProfileName is a default profile name if not selected | ||
const DefaultProfileName = `default` | ||
var currentCmd = &cobra.Command{ | ||
Use: "current [output template]", | ||
Short: "Show current git config keys", | ||
Long: "Command output git config keys in a format specified by output template (Go template) that was passed.", | ||
Example: `# Show current use.email | ||
$ git-profile-manager current "{{ .user.email }}" | ||
test@test.com | ||
# Show current name and email | ||
$ git-profile-manager current "{{ user.name }} ({{ user.email }})" | ||
Test Name (test@test.com)`, | ||
Args: cobra.ExactArgs(1), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
tpl := args[0] | ||
fmt.Println(templateRender(tpl)) | ||
}, | ||
} | ||
|
||
var currentCmd = &cobra.Command{ //nolint:gochecknoglobals | ||
Use: "current", | ||
Aliases: []string{"c"}, | ||
Short: "Show selected profile", | ||
Long: "Show selected profile for current repository.", | ||
Run: currentRun, | ||
func init() { | ||
rootCmd.AddCommand(currentCmd) | ||
} | ||
|
||
func currentRun(cmd *cobra.Command, args []string) { | ||
if len(cfgStorage.Profiles) == 0 || !git.IsRepository() { | ||
os.Exit(1) | ||
func getConfigValue(key string) string { | ||
out, err := gitconfig.Get(gitconfig.MergedConfig, key) | ||
if err != nil { | ||
return "" | ||
} | ||
return out[:len(out)-1] | ||
} | ||
|
||
res, err := git.GetLocalConfig(`current-profile.name`) | ||
if len(res) == 0 || err != nil { | ||
cmd.Print(DefaultProfileName) | ||
os.Exit(0) | ||
} | ||
//nolint:gocyclo | ||
func templateRender(tpl string) string { | ||
phStartPos := -1 | ||
phEndPos := 0 | ||
keyStartPos := -1 | ||
keyEndPos := -1 | ||
result := "" | ||
|
||
for i := 0; i < len(tpl); i++ { | ||
if i != 0 && tpl[i] == '{' && tpl[i-1] == '{' { | ||
phStartPos = i - 1 | ||
continue | ||
} | ||
|
||
cmd.Printf("%s", res) | ||
if phStartPos != -1 && keyStartPos == -1 && tpl[i] != ' ' { | ||
keyStartPos = i | ||
} | ||
|
||
if keyStartPos != -1 && keyEndPos == -1 && tpl[i] == ' ' { | ||
keyEndPos = i | ||
} | ||
|
||
if i != 0 && tpl[i] == '}' && tpl[i-1] == '}' { | ||
if phStartPos != -1 { | ||
result += tpl[phEndPos:phStartPos] | ||
if keyEndPos == -1 { | ||
keyEndPos = i - 1 | ||
} | ||
result += getConfigValue(tpl[keyStartPos:keyEndPos]) | ||
phEndPos = i + 1 | ||
phStartPos = -1 | ||
keyStartPos = -1 | ||
keyEndPos = -1 | ||
} | ||
} | ||
} | ||
result += tpl[phEndPos:] | ||
return result | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.