github.com/adharshmk96/stk@v1.2.3/pkg/project/helpers.go (about) 1 package project 2 3 import ( 4 "errors" 5 "fmt" 6 "math/rand" 7 "os" 8 "strings" 9 "time" 10 11 "github.com/adharshmk96/stk/pkg/commands" 12 ) 13 14 func Clean(output string, err error) (string, error) { 15 output = strings.ReplaceAll(strings.Split(output, "\n")[0], "'", "") 16 if err != nil { 17 err = errors.New(strings.TrimSuffix(err.Error(), "\n")) 18 } 19 return output, err 20 } 21 22 func OpenDirectory(workDir string) error { 23 if err := os.MkdirAll(workDir, 0755); err != nil { 24 return err 25 } 26 return os.Chdir(workDir) 27 } 28 29 func GitRepoName() (string, error) { 30 gitCmd := commands.NewGitCmd() 31 remoteUrl, err := Clean(gitCmd.GetRemoteOrigin()) 32 if err != nil { 33 return "", err 34 } 35 36 repoUrl := strings.TrimSuffix(remoteUrl, ".git") 37 repoUrl = strings.ReplaceAll(repoUrl, "https://", "") 38 repoUrl = strings.ReplaceAll(repoUrl, "git@", "") 39 repoUrl = strings.ReplaceAll(repoUrl, ":", "/") 40 41 return repoUrl, nil 42 } 43 44 func getFirstArg(args []string) string { 45 if len(args) > 0 { 46 return strings.TrimSpace(args[0]) 47 } 48 return "" 49 } 50 51 func RandomName() string { 52 nouns := []string{"apple", "ball", "cat", "dog", "elephant", "fish", "gorilla", "horse", "iguana", "jellyfish", "kangaroo"} 53 adjectives := []string{"angry", "big", "cold", "dark", "fast", "good", "happy", "jolly", "kind", "little", "merry", "nice"} 54 55 randSrc := rand.NewSource(time.Now().UnixNano()) 56 randGen := rand.New(randSrc) 57 58 return fmt.Sprintf("%s%s", adjectives[randGen.Intn(len(adjectives))], nouns[randGen.Intn(len(nouns))]) 59 }