github.com/LGUG2Z/story@v0.4.1/cli/commit.go (about) 1 package cli 2 3 import ( 4 "fmt" 5 "sort" 6 "strings" 7 8 "github.com/LGUG2Z/story/git" 9 "github.com/LGUG2Z/story/manifest" 10 "github.com/spf13/afero" 11 "github.com/urfave/cli" 12 ) 13 14 func CommitCmd(fs afero.Fs) cli.Command { 15 return cli.Command{ 16 Name: "commit", 17 Usage: "Commits code across the current story", 18 Flags: []cli.Flag{ 19 cli.StringFlag{Name: "message, m", Usage: "Commit message"}, 20 }, 21 Action: cli.ActionFunc(func(c *cli.Context) error { 22 if !isStory { 23 return ErrNotWorkingOnAStory 24 } 25 26 if c.Args().Present() { 27 return ErrCommandTakesNoArguments 28 } 29 30 story, err := manifest.LoadStory(fs) 31 if err != nil { 32 return err 33 } 34 35 // Commit in all the projects 36 messages := []string{fmt.Sprintf("[story commit] %s", c.String("message"))} 37 for project := range story.Projects { 38 output, err := git.Commit(git.CommitOpts{Project: project, Messages: messages}) 39 if err != nil { 40 return err 41 } 42 43 printGitOutput(output, project) 44 } 45 46 // Update the hashes in the meta file and write it out 47 hashes, err := story.GetCommitHashes(fs) 48 if err != nil { 49 return err 50 } 51 52 story.Hashes = hashes 53 if err := story.Write(fs); err != nil { 54 return err 55 } 56 57 // Format the hashes to the GitHub format to link to a specific commit 58 var hashMessages []string 59 for project, hash := range hashes { 60 // TODO: switch depending on GitHub or GitLab for now. Maybe more later 61 commitUrl := fmt.Sprintf("https://github.com/%s/%s/commit/%s", story.Orgranisation, project, hash) 62 hashMessages = append(hashMessages, commitUrl) 63 } 64 65 // Add the hashes to the slice for git commit messages 66 sort.Strings(hashMessages) 67 messages = append(messages, strings.Join(hashMessages, "\n")) 68 69 // Add the blast radius to the slice for git commit messages 70 var brMap = make(map[string]bool) 71 72 for _, br := range story.BlastRadius { 73 for _, p := range br { 74 if !brMap[p] { 75 brMap[p] = true 76 } 77 } 78 } 79 80 var brSlice []string 81 for project := range brMap { 82 brSlice = append(brSlice, project) 83 } 84 85 messages = append(messages, fmt.Sprintf("Blast Radius: %s", strings.Join(brSlice, " "))) 86 87 // Stage the story file 88 output, err := git.Add(git.AddOpts{Files: []string{".meta"}}) 89 if err != nil { 90 return err 91 } 92 93 // Commit on the metarepo 94 output, err = git.Commit(git.CommitOpts{Messages: messages}) 95 if err != nil { 96 return err 97 } 98 99 printGitOutput(output, metarepo) 100 101 return nil 102 }), 103 } 104 }