├── .gitignore ├── README.md ├── actions ├── Clone.go ├── Config.go ├── Create.go ├── Import.go ├── Show.go └── ShowKey.go ├── build.sh ├── git ├── PatchGitConfig.go └── QueryGitConfig.go ├── go.mod ├── install.sh ├── main.go ├── profiles ├── Profile.go ├── Profiles.go ├── Read.go └── Save.go └── prompt └── String.go /.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # git-identity 2 | 3 | This CLI tool is a complementary `git extension` to allow better 4 | management and integration of multiple git user profiles. 5 | 6 | The git command line is pain to use, hard to remember, and easy 7 | to get wrong. That's why this tool exists. 8 | 9 | 10 | ## Syntax 11 | 12 | | Action | Description | 13 | |:-------------------|:----------------------------------------------------| 14 | | `create ` | creates a new alias | 15 | | `import ` | imports global git user config as a new alias | 16 | | `clone ` | clones a private repository with SSH key of alias | 17 | | `config ` | modifies current git repository to always use alias | 18 | | `show` | prints out available aliases | 19 | | `show-key ` | prints out the public SSH key for given alias | 20 | 21 | 22 | ## Examples 23 | 24 | ```bash 25 | # import global git/ssh config as new alias 26 | git identity import cookiengineer; 27 | 28 | # create an alias 29 | git identity create another-alias; 30 | 31 | # clone a repo via an aliases' SSH key 32 | git identity clone cookiengineer git@github.com:cookiengineer/private-repository.git ./private-repo; 33 | 34 | # modify alias usage inside a repo 35 | cd ./private-repo; 36 | git identity config another-alias; 37 | git push origin master; # uses another-alias automatically! 38 | 39 | # easy copy/paste of public key to web apps 40 | git identity show-key another-alias; 41 | ``` 42 | 43 | 44 | ## Installation 45 | 46 | ```bash 47 | # clone the repository 48 | git clone https://github.com/cookiengineer/git-identity; 49 | 50 | # builds and installs the binary to /usr/bin/git-identity 51 | cd ./git-identity; 52 | bash install.sh; 53 | ``` 54 | 55 | 56 | ## Filesystem Backup / Config Folders 57 | 58 | - The compiled [main.go](/main.go) binary has to be installed as `/usr/bin/git-identity` 59 | - `$XDG_CONFIG_HOME/git-identity` is the preferred config folder 60 | - `$HOME/.config/git-identity` is the first fallback config folder 61 | - `/home/$USER/.config/git-identity` is the second fallback config folder 62 | 63 | 64 | ## License 65 | 66 | WTFPL 67 | 68 | -------------------------------------------------------------------------------- /actions/Clone.go: -------------------------------------------------------------------------------- 1 | package actions 2 | 3 | import "git-identity/git" 4 | import "git-identity/profiles" 5 | import "os" 6 | import "os/exec" 7 | import "strings" 8 | import "fmt" 9 | 10 | func Clone(alias string, arguments []string) bool { 11 | 12 | var result bool = false 13 | 14 | profile, ok := profiles.Profiles[alias] 15 | 16 | if ok == true { 17 | 18 | err1 := os.Setenv("GIT_SSH_COMMAND", profile.Core.SSHCommand) 19 | 20 | if err1 == nil { 21 | 22 | cmd_arguments := []string{"clone"} 23 | cmd_arguments = append(cmd_arguments, arguments...) 24 | 25 | var stdout_buf strings.Builder 26 | var stderr_buf strings.Builder 27 | 28 | cmd := exec.Command("git", cmd_arguments...) 29 | cmd.Stdout = &stdout_buf 30 | cmd.Stderr = &stderr_buf 31 | 32 | err2 := cmd.Run() 33 | 34 | if err2 == nil { 35 | 36 | buffer := strings.TrimSpace(stderr_buf.String()) 37 | lines := strings.Split(buffer, "\n") 38 | 39 | if len(lines) > 0 { 40 | 41 | if strings.HasPrefix(lines[0], "Cloning into '") && strings.HasSuffix(lines[0], "'...") { 42 | 43 | repo_folder := lines[0][14:len(lines[0])-4] 44 | 45 | var git_folder string 46 | 47 | if strings.HasPrefix(repo_folder, "./") { 48 | 49 | pwd, err := os.Getwd() 50 | 51 | if err == nil { 52 | git_folder = pwd+"/"+repo_folder[2:]+"/.git" 53 | } else { 54 | git_folder = repo_folder+"/.git" 55 | } 56 | 57 | } else if strings.HasPrefix(repo_folder, "/") { 58 | git_folder = repo_folder+"/.git" 59 | } 60 | 61 | if git_folder != "" { 62 | 63 | result = git.PatchGitConfig(git_folder+"/config", profile) 64 | 65 | } else { 66 | 67 | fmt.Println("Warning: Please execute git identity config \"" + alias + "\" manually inside the repository folder.") 68 | result = true 69 | 70 | } 71 | 72 | } 73 | 74 | } 75 | 76 | } 77 | 78 | } 79 | 80 | } 81 | 82 | return result 83 | 84 | } 85 | -------------------------------------------------------------------------------- /actions/Config.go: -------------------------------------------------------------------------------- 1 | package actions 2 | 3 | import "git-identity/git" 4 | import "git-identity/profiles" 5 | import "fmt" 6 | import "os/exec" 7 | import "strings" 8 | 9 | func Config(alias string) bool { 10 | 11 | var result bool = false 12 | 13 | profile, ok := profiles.Profiles[alias] 14 | 15 | if ok == true { 16 | 17 | cmd1 := exec.Command("git", "rev-parse", "--absolute-git-dir") 18 | buffer1, err1 := cmd1.Output() 19 | 20 | if err1 == nil { 21 | 22 | git_folder := strings.TrimSpace(string(buffer1)) 23 | 24 | if git_folder != "" { 25 | result = git.PatchGitConfig(git_folder+"/config", profile) 26 | } 27 | 28 | } else { 29 | 30 | fmt.Println("Warning: Not a git repository!") 31 | result = false 32 | 33 | } 34 | 35 | } 36 | 37 | return result 38 | 39 | } 40 | -------------------------------------------------------------------------------- /actions/Create.go: -------------------------------------------------------------------------------- 1 | package actions 2 | 3 | import "git-identity/profiles" 4 | import "git-identity/prompt" 5 | import "fmt" 6 | import "os/exec" 7 | 8 | func Create(alias string) bool { 9 | 10 | var result bool = false 11 | 12 | _, ok := profiles.Profiles[alias] 13 | 14 | if ok == false { 15 | 16 | name := prompt.String("What is your full username?") 17 | email := prompt.String("What is your email address?") 18 | 19 | key := profiles.Folder+"/"+alias+".key" 20 | cmd := exec.Command("ssh-keygen", "-t", "ed25519", "-C", email, "-f", key, "-q", "-N", "") 21 | _, err := cmd.Output() 22 | 23 | if err == nil { 24 | 25 | var profile profiles.Profile 26 | 27 | profile.Core.SSHCommand="ssh -i \""+key+"\" -F /dev/null" 28 | profile.User.Name = name 29 | profile.User.Email = email 30 | profile.User.UseConfigOnly = true 31 | 32 | result = profiles.Save(alias, profile) 33 | 34 | } else { 35 | 36 | fmt.Println("Warning: Could not generate SSH key pair!") 37 | fmt.Println("Warning: Please execute ssh-keygen -t ed25519 -f \"" + key + "\" manually.") 38 | result = false 39 | 40 | } 41 | 42 | } else { 43 | 44 | fmt.Println("Warning: Identity \"" + alias + "\" already exists!") 45 | result = false 46 | 47 | } 48 | 49 | return result 50 | 51 | } 52 | -------------------------------------------------------------------------------- /actions/Import.go: -------------------------------------------------------------------------------- 1 | package actions 2 | 3 | import "git-identity/git" 4 | import "git-identity/profiles" 5 | import "git-identity/prompt" 6 | import "fmt" 7 | import "io/ioutil" 8 | import "os" 9 | 10 | var key_names []string = []string{ 11 | "id_dsa", 12 | "id_ecdsa", 13 | "id_ecdsa_sk", 14 | "id_ed25519", 15 | "id_ed25519_sk", 16 | "id_rsa", 17 | } 18 | 19 | func Import(alias string) bool { 20 | 21 | var result bool = false 22 | 23 | _, ok := profiles.Profiles[alias] 24 | 25 | if ok == false { 26 | 27 | var ssh_folder string 28 | var git_config string 29 | 30 | var result_ssh bool = false 31 | var result_git bool = false 32 | 33 | home := os.Getenv("HOME") 34 | user := os.Getenv("USER") 35 | 36 | if home != "" { 37 | git_config = home + "/.gitconfig" 38 | ssh_folder = home + "/.ssh" 39 | } else if user != "" { 40 | git_config = "/home/" + user + "/.gitconfig" 41 | ssh_folder = "/home/" + user + "/.ssh" 42 | } 43 | 44 | if ssh_folder != "" { 45 | 46 | for k := 0; k < len(key_names); k++ { 47 | 48 | in_key := ssh_folder + "/" + key_names[k] 49 | out_key := profiles.Folder+"/"+alias+".key" 50 | 51 | stat, err1 := os.Stat(in_key) 52 | 53 | if err1 == nil && stat.IsDir() == false { 54 | 55 | private_key_buffer, err2 := ioutil.ReadFile(in_key) 56 | public_key_buffer, err3 := ioutil.ReadFile(in_key+".pub") 57 | 58 | if err2 == nil && err3 == nil { 59 | 60 | err4 := ioutil.WriteFile(out_key, private_key_buffer, 0600) 61 | err5 := ioutil.WriteFile(out_key+".pub", public_key_buffer, 0644) 62 | 63 | if err4 == nil && err5 == nil { 64 | result_ssh = true 65 | } 66 | 67 | } else { 68 | 69 | fmt.Println("Warning: Could not import SSH key pair!") 70 | result = false 71 | 72 | } 73 | 74 | break 75 | 76 | } 77 | 78 | } 79 | 80 | } 81 | 82 | if git_config != "" { 83 | 84 | name := git.QueryGitConfig(git_config, "user.name") 85 | email := git.QueryGitConfig(git_config, "user.email") 86 | key := profiles.Folder+"/"+alias+".key" 87 | 88 | if name == "" { 89 | name = prompt.String("What is your full username?") 90 | } 91 | 92 | if email == "" { 93 | email = prompt.String("What is your email address?") 94 | } 95 | 96 | if name != "" && email != "" { 97 | 98 | var profile profiles.Profile 99 | 100 | profile.Core.SSHCommand="ssh -i \""+key+"\" -F /dev/null" 101 | profile.User.Name = name 102 | profile.User.Email = email 103 | profile.User.UseConfigOnly = true 104 | 105 | result_git = profiles.Save(alias, profile) 106 | 107 | } 108 | 109 | } 110 | 111 | if result_ssh == true && result_git == true { 112 | result = true 113 | } 114 | 115 | } else { 116 | 117 | fmt.Println("Warning: Identity \"" + alias + "\" already exists!") 118 | result = false 119 | 120 | } 121 | 122 | return result 123 | 124 | } 125 | -------------------------------------------------------------------------------- /actions/Show.go: -------------------------------------------------------------------------------- 1 | package actions 2 | 3 | import "git-identity/git" 4 | import "git-identity/profiles" 5 | import "fmt" 6 | import "os/exec" 7 | import "sort" 8 | import "strings" 9 | 10 | func Show() bool { 11 | 12 | var active string 13 | var aliases []string 14 | 15 | for alias, _ := range profiles.Profiles { 16 | aliases = append(aliases, alias) 17 | } 18 | 19 | sort.Strings(aliases) 20 | 21 | cmd1 := exec.Command("git", "rev-parse", "--absolute-git-dir") 22 | buffer1, err1 := cmd1.Output() 23 | 24 | if err1 == nil { 25 | 26 | git_folder := strings.TrimSpace(string(buffer1)) 27 | 28 | if git_folder != "" { 29 | 30 | git_config := git_folder + "/config" 31 | name := git.QueryGitConfig(git_config, "user.name") 32 | email := git.QueryGitConfig(git_config, "user.email") 33 | 34 | for alias, profile := range profiles.Profiles { 35 | 36 | if profile.User.Name == name && profile.User.Email == email { 37 | active = alias 38 | break 39 | } 40 | 41 | } 42 | 43 | } 44 | 45 | } 46 | 47 | for a := 0; a < len(aliases); a++ { 48 | 49 | alias := aliases[a] 50 | 51 | if alias == active { 52 | fmt.Println("* "+alias) 53 | } else { 54 | fmt.Println(" "+alias) 55 | } 56 | 57 | } 58 | 59 | return true 60 | 61 | } 62 | -------------------------------------------------------------------------------- /actions/ShowKey.go: -------------------------------------------------------------------------------- 1 | package actions 2 | 3 | import "git-identity/profiles" 4 | import "fmt" 5 | import "os" 6 | import "strings" 7 | 8 | func ShowKey(alias string) bool { 9 | 10 | var result bool = false 11 | 12 | _, ok := profiles.Profiles[alias] 13 | 14 | if ok == true { 15 | 16 | key := profiles.Folder+"/"+alias+".key.pub" 17 | 18 | buffer, err := os.ReadFile(key) 19 | 20 | if err == nil { 21 | 22 | fmt.Println(strings.TrimSpace(string(buffer))) 23 | result = true 24 | 25 | } else { 26 | 27 | fmt.Println("Warning: Identity \"" + alias + "\" has no SSH key pair!?") 28 | fmt.Println("Warning: Please execute ssh-keygen -t ed25519 -f \"" + key + "\" manually.") 29 | result = false 30 | 31 | } 32 | 33 | } 34 | 35 | return result 36 | 37 | } 38 | -------------------------------------------------------------------------------- /build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | GO="$(which go)"; 4 | ROOT="$(pwd)"; 5 | SUDO="$(which sudo)"; 6 | 7 | 8 | build() { 9 | 10 | local os="${1}"; 11 | local arch="${2}"; 12 | local folder="${ROOT}/build"; 13 | 14 | if [[ ! -d "${folder}" ]]; then 15 | mkdir -p "${folder}"; 16 | fi; 17 | 18 | cd "${ROOT}"; 19 | 20 | if [[ "${os}" == "windows" ]]; then 21 | env CGO_ENABLED=0 ${GO} build -o "${folder}/git-identity_${os}_${arch}.exe" "${ROOT}/main.go"; 22 | else 23 | env CGO_ENABLED=0 ${GO} build -o "${folder}/git-identity_${os}_${arch}" "${ROOT}/main.go"; 24 | fi; 25 | 26 | } 27 | 28 | build "windows" "amd64"; 29 | 30 | build "linux" "amd64"; 31 | build "linux" "arm"; 32 | build "linux" "arm64"; 33 | 34 | build "darwin" "amd64"; 35 | build "darwin" "arm64"; 36 | 37 | build "freebsd" "amd64"; 38 | build "freebsd" "arm64"; 39 | 40 | build "openbsd" "amd64"; 41 | build "openbsd" "arm64"; 42 | 43 | -------------------------------------------------------------------------------- /git/PatchGitConfig.go: -------------------------------------------------------------------------------- 1 | package git 2 | 3 | import "git-identity/profiles" 4 | import "os/exec" 5 | 6 | func PatchGitConfig(git_config string, profile profiles.Profile) bool { 7 | 8 | var result bool = false 9 | 10 | if git_config != "" { 11 | 12 | var result_core_sshcommand bool = false 13 | var result_user_name bool = false 14 | var result_user_email bool = false 15 | 16 | if profile.User.Name != "" { 17 | 18 | cmd := exec.Command("git", "config", "--file", git_config, "user.name", profile.User.Name) 19 | _, err := cmd.Output() 20 | 21 | if err == nil { 22 | result_user_name = true 23 | } 24 | 25 | } else { 26 | result_user_name = true 27 | } 28 | 29 | if profile.User.Email != "" { 30 | 31 | cmd := exec.Command("git", "config", "--file", git_config, "user.email", profile.User.Email) 32 | _, err := cmd.Output() 33 | 34 | if err == nil { 35 | result_user_email = true 36 | } 37 | 38 | } else { 39 | result_user_email = true 40 | } 41 | 42 | if profile.Core.SSHCommand != "" { 43 | 44 | cmd := exec.Command("git", "config", "--file", git_config, "core.sshCommand", profile.Core.SSHCommand) 45 | _, err := cmd.Output() 46 | 47 | if err == nil { 48 | result_core_sshcommand = true 49 | } 50 | 51 | } else { 52 | result_core_sshcommand = true 53 | } 54 | 55 | if result_user_name == true && result_user_email == true && result_core_sshcommand == true { 56 | result = true 57 | } 58 | 59 | } 60 | 61 | return result 62 | 63 | } 64 | -------------------------------------------------------------------------------- /git/QueryGitConfig.go: -------------------------------------------------------------------------------- 1 | package git 2 | 3 | import "os/exec" 4 | import "strings" 5 | 6 | func QueryGitConfig(git_config string, query string) string { 7 | 8 | var result string 9 | 10 | cmd := exec.Command("git", "config", "--file", git_config, query) 11 | buffer, err := cmd.Output() 12 | 13 | if err == nil { 14 | result = strings.TrimSpace(string(buffer)) 15 | } 16 | 17 | return result 18 | 19 | } 20 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module git-identity 2 | 3 | go 1.20 4 | -------------------------------------------------------------------------------- /install.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | GO="$(which go)"; 4 | ROOT="$(pwd)"; 5 | SUDO="$(which sudo)"; 6 | 7 | 8 | 9 | build_and_install() { 10 | 11 | cd "${ROOT}"; 12 | 13 | env CGO_ENABLED=0 ${GO} build -o "/tmp/git-identity" "${ROOT}/main.go"; 14 | 15 | if [[ "$?" == "0" ]]; then 16 | ${SUDO} mv "/tmp/git-identity" "/usr/bin/git-identity"; 17 | ${SUDO} chmod +x "/usr/bin/git-identity"; 18 | fi; 19 | 20 | } 21 | 22 | build_and_install; 23 | 24 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "git-identity/actions" 4 | import "os" 5 | import "strings" 6 | import "fmt" 7 | 8 | func showUsage() { 9 | 10 | fmt.Println("Usage: git-identity [Action] [Alias] [...]") 11 | fmt.Println("") 12 | fmt.Println("| Action | Description |") 13 | fmt.Println("|:-----------------|:----------------------------------------------------|") 14 | fmt.Println("| create | creates a new alias |") 15 | fmt.Println("| import | imports global git user config as a new alias |") 16 | fmt.Println("| clone | clones a private repository with SSH key of alias |") 17 | fmt.Println("| config | modifies current git repository to always use alias |") 18 | fmt.Println("| show | prints out available aliases |") 19 | fmt.Println("| show-key | prints out the public SSH key for given alias |") 20 | fmt.Println("") 21 | fmt.Println("Examples:") 22 | fmt.Println("") 23 | fmt.Println("# 1.A) create a new alias") 24 | fmt.Println("git identity create us3rn4me;") 25 | fmt.Println("") 26 | fmt.Println("# 1.B) import a new alias from existing global ssh/git config") 27 | fmt.Println("git identity import us3rn4me;") 28 | fmt.Println("") 29 | fmt.Println("# 2. copy/paste the ssh public key to github/gitlab") 30 | fmt.Println("git identity show-key us3rn4m3;") 31 | fmt.Println("") 32 | fmt.Println("# 3. clone a private repository with the given alias") 33 | fmt.Println("git identity clone us3rn4m3 git@github.com:private/repo.git ./repo;") 34 | fmt.Println("") 35 | fmt.Println("# 4. modify repository config use the given alias") 36 | fmt.Println("cd /path/to/existing/repo;") 37 | fmt.Println("git identity config us3rn4m3;") 38 | 39 | } 40 | 41 | func main() { 42 | 43 | var alias string 44 | var action string 45 | var arguments []string 46 | 47 | if len(os.Args) == 2 { 48 | 49 | action = strings.TrimSpace(os.Args[1]) 50 | 51 | } else if len(os.Args) == 3 { 52 | 53 | action = strings.TrimSpace(os.Args[1]) 54 | alias = strings.TrimSpace(os.Args[2]) 55 | 56 | } else if len(os.Args) > 3 { 57 | 58 | action = strings.TrimSpace(os.Args[1]) 59 | alias = strings.TrimSpace(os.Args[2]) 60 | arguments = os.Args[3:] 61 | 62 | } 63 | 64 | if action == "create" { 65 | 66 | if alias != "" { 67 | 68 | result := actions.Create(alias) 69 | 70 | if result == true { 71 | os.Exit(0) 72 | } else { 73 | os.Exit(1) 74 | } 75 | 76 | } else { 77 | 78 | showUsage() 79 | os.Exit(1) 80 | 81 | } 82 | 83 | } else if action == "import" { 84 | 85 | if alias != "" { 86 | 87 | result := actions.Import(alias) 88 | 89 | if result == true { 90 | os.Exit(0) 91 | } else { 92 | os.Exit(1) 93 | } 94 | 95 | } else { 96 | 97 | showUsage() 98 | os.Exit(1) 99 | 100 | } 101 | 102 | } else if action == "clone" { 103 | 104 | if alias != "" { 105 | 106 | result := actions.Clone(alias, arguments) 107 | 108 | if result == true { 109 | os.Exit(0) 110 | } else { 111 | os.Exit(1) 112 | } 113 | 114 | } else { 115 | 116 | showUsage() 117 | os.Exit(1) 118 | 119 | } 120 | 121 | } else if action == "config" { 122 | 123 | if alias != "" { 124 | 125 | result := actions.Config(alias) 126 | 127 | if result == true { 128 | os.Exit(0) 129 | } else { 130 | os.Exit(1) 131 | } 132 | 133 | } else { 134 | 135 | showUsage() 136 | os.Exit(1) 137 | 138 | } 139 | 140 | } else if action == "show" { 141 | 142 | result := actions.Show() 143 | 144 | if result == true { 145 | os.Exit(0) 146 | } else { 147 | os.Exit(1) 148 | } 149 | 150 | } else if action == "showkey" || action == "show-key" { 151 | 152 | if alias != "" { 153 | 154 | result := actions.ShowKey(alias) 155 | 156 | if result == true { 157 | os.Exit(0) 158 | } else { 159 | os.Exit(1) 160 | } 161 | 162 | } else { 163 | 164 | showUsage() 165 | os.Exit(1) 166 | 167 | } 168 | 169 | } else { 170 | 171 | showUsage() 172 | os.Exit(1) 173 | 174 | } 175 | 176 | } 177 | -------------------------------------------------------------------------------- /profiles/Profile.go: -------------------------------------------------------------------------------- 1 | package profiles 2 | 3 | type Profile struct { 4 | Core struct { 5 | SSHCommand string `json:"sshCommand"` 6 | } `json:"core"` 7 | User struct { 8 | Name string `json:"name"` 9 | Email string `json:"email"` 10 | UseConfigOnly bool `json:"useConfigOnly"` 11 | } `json:"user"` 12 | } 13 | -------------------------------------------------------------------------------- /profiles/Profiles.go: -------------------------------------------------------------------------------- 1 | package profiles 2 | 3 | import "os" 4 | import "strings" 5 | 6 | var Folder string 7 | var Profiles map[string]Profile 8 | 9 | func init() { 10 | 11 | Profiles = make(map[string]Profile, 0) 12 | 13 | var folder string 14 | 15 | xdg_config := os.Getenv("XDG_CONFIG_HOME") 16 | home := os.Getenv("HOME") 17 | user := os.Getenv("USER") 18 | 19 | if xdg_config != "" { 20 | folder = xdg_config + "/git-identity" 21 | } else if home != "" { 22 | folder = home + "/.config/git-identity" 23 | } else if user != "" { 24 | folder = "/home/" + user + "/.config/git-identity" 25 | } 26 | 27 | 28 | if folder != "" { 29 | 30 | Folder = folder 31 | 32 | stat, err1 := os.Stat(folder) 33 | 34 | if err1 == nil && stat.IsDir() { 35 | 36 | entries, err2 := os.ReadDir(folder) 37 | 38 | if err2 == nil { 39 | 40 | for e := 0; e < len(entries); e++ { 41 | 42 | var file = entries[e].Name() 43 | 44 | if strings.HasSuffix(file, ".json") { 45 | Read(file[0:len(file)-5]) 46 | } 47 | 48 | } 49 | 50 | } 51 | 52 | } else { 53 | os.MkdirAll(folder, 0750) 54 | } 55 | 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /profiles/Read.go: -------------------------------------------------------------------------------- 1 | package profiles 2 | 3 | import "encoding/json" 4 | import "os" 5 | 6 | func Read(alias string) bool { 7 | 8 | var result bool = false 9 | 10 | if Folder != "" && alias != "" { 11 | 12 | buffer, err1 := os.ReadFile(Folder+"/"+alias+".json") 13 | 14 | if err1 == nil { 15 | 16 | var profile Profile 17 | 18 | err2 := json.Unmarshal(buffer, &profile) 19 | 20 | 21 | if err2 == nil { 22 | Profiles[alias] = profile 23 | result = true 24 | } 25 | 26 | } 27 | 28 | } 29 | 30 | return result 31 | 32 | } 33 | -------------------------------------------------------------------------------- /profiles/Save.go: -------------------------------------------------------------------------------- 1 | package profiles 2 | 3 | import "encoding/json" 4 | import "os" 5 | import "fmt" 6 | 7 | func Save(alias string, profile Profile) bool { 8 | 9 | var result bool = false 10 | 11 | if Folder != "" && alias != "" { 12 | 13 | buffer, err1 := json.MarshalIndent(profile, "", "\t") 14 | 15 | if err1 == nil { 16 | 17 | err2 := os.WriteFile(Folder+"/"+alias+".json", buffer, 0666) 18 | 19 | if err2 == nil { 20 | result = true 21 | } else { 22 | fmt.Println("Warning: Could not save config to \""+Folder+"/"+alias+".json\"!") 23 | } 24 | 25 | } else { 26 | fmt.Println("Warning: Could not save config to \""+Folder+"/"+alias+".json\"!") 27 | } 28 | 29 | } 30 | 31 | return result 32 | 33 | } 34 | -------------------------------------------------------------------------------- /prompt/String.go: -------------------------------------------------------------------------------- 1 | package prompt 2 | 3 | import "bufio" 4 | import "fmt" 5 | import "os" 6 | import "strings" 7 | 8 | func String(question string) string { 9 | 10 | var answer string 11 | 12 | reader := bufio.NewReader(os.Stdin) 13 | 14 | for { 15 | 16 | fmt.Println(question+" ") 17 | 18 | tmp1, err := reader.ReadString('\n') 19 | 20 | if err == nil && tmp1 != "" { 21 | 22 | tmp2 := strings.TrimSpace(tmp1) 23 | 24 | if tmp2 != "" { 25 | answer = tmp2 26 | break 27 | } 28 | 29 | } 30 | 31 | } 32 | 33 | return answer 34 | 35 | } 36 | 37 | --------------------------------------------------------------------------------