22package cmd
33
44import (
5- "errors"
65 "fmt"
76 "os"
87 "time"
98
109 "github.com/spf13/cobra"
1110
1211 "github.com/kalverra/octometrics/gather"
12+ "github.com/kalverra/octometrics/observe"
1313)
1414
1515var (
16- githubToken string
17- forceUpdate bool
16+ githubToken string
17+ githubClient * gather.GitHubClient
18+ forceUpdate bool
19+ noObserve bool
1820)
1921
2022var gatherCmd = & cobra.Command {
2123 Use : "gather" ,
2224 Short : "Gather metrics from GitHub" ,
2325 PreRunE : func (_ * cobra.Command , _ []string ) error {
24- if owner == "" {
25- return errors .New ("owner must be provided" )
26- }
27- if repo == "" {
28- return errors .New ("repo must be provided" )
29- }
30-
31- setCount := 0
32- if commitSHA != "" {
33- setCount ++
34- }
35- if workflowRunID != 0 {
36- setCount ++
37- }
38- if pullRequestNumber != 0 {
39- setCount ++
40- }
41- if setCount > 1 {
42- return errors .New ("only one of commit SHA, workflow run ID or pull request number can be provided" )
26+ if err := cfg .ValidateGather (); err != nil {
27+ return err
4328 }
44- if setCount == 0 {
45- return errors .New ("one of commit SHA, workflow run ID or pull request number must be provided" )
29+ var err error
30+ githubClient , err = gather .NewGitHubClient (logger , githubToken , nil )
31+ if err != nil {
32+ return fmt .Errorf ("failed to create GitHub client: %w" , err )
4633 }
47-
4834 return nil
4935 },
5036 RunE : func (_ * cobra.Command , _ []string ) error {
@@ -54,15 +40,16 @@ var gatherCmd = &cobra.Command{
5440
5541 startTime := time .Now ()
5642
57- logger = logger .With ().Str ("owner" , owner ).Str ("repo" , repo ).Logger ()
43+ logger = logger .With ().Str ("owner" , cfg . Owner ).Str ("repo" , cfg . Repo ).Logger ()
5844
59- if workflowRunID != 0 {
60- logger = logger .With ().Int64 ("workflow_run_id" , workflowRunID ).Logger ()
61- } else if pullRequestNumber != 0 {
62- logger = logger .With ().Int ("pull_request_number" , pullRequestNumber ).Logger ()
45+ if cfg . WorkflowRunID != 0 {
46+ logger = logger .With ().Int64 ("workflow_run_id" , cfg . WorkflowRunID ).Logger ()
47+ } else if cfg . PullRequestNumber != 0 {
48+ logger = logger .With ().Int ("pull_request_number" , cfg . PullRequestNumber ).Logger ()
6349 }
6450
6551 logger .Info ().Msg ("Gathering data" )
52+ fmt .Println ("Gathering data..." )
6653
6754 opts := []gather.Option {}
6855
@@ -71,40 +58,50 @@ var gatherCmd = &cobra.Command{
7158 }
7259
7360 var err error
74- if workflowRunID != 0 {
75- _ , _ , err = gather .WorkflowRun (logger , githubClient , owner , repo , workflowRunID , opts ... )
76- } else if pullRequestNumber != 0 {
77- _ , err = gather .PullRequest (logger , githubClient , owner , repo , pullRequestNumber , opts ... )
78- } else if commitSHA != "" {
79- _ , err = gather .Commit (logger , githubClient , owner , repo , commitSHA , opts ... )
61+ if cfg . WorkflowRunID != 0 {
62+ _ , _ , err = gather .WorkflowRun (logger , githubClient , cfg . Owner , cfg . Repo , cfg . WorkflowRunID , opts ... )
63+ } else if cfg . PullRequestNumber != 0 {
64+ _ , err = gather .PullRequest (logger , githubClient , cfg . Owner , cfg . Repo , cfg . PullRequestNumber , opts ... )
65+ } else if cfg . CommitSHA != "" {
66+ _ , err = gather .Commit (logger , githubClient , cfg . Owner , cfg . Repo , cfg . CommitSHA , opts ... )
8067 }
8168 if err != nil {
8269 return err
8370 }
8471
8572 logger .Info ().Str ("duration" , time .Since (startTime ).String ()).Msg ("Gathered data" )
86- return nil
73+ fmt .Println ("Gathered data" )
74+
75+ if noObserve {
76+ return nil
77+ }
78+
79+ var pagePath string
80+ if cfg .WorkflowRunID != 0 {
81+ pagePath = fmt .Sprintf ("/%s/%s/workflow_runs/%d.html" , cfg .Owner , cfg .Repo , cfg .WorkflowRunID )
82+ } else if cfg .PullRequestNumber != 0 {
83+ pagePath = fmt .Sprintf ("/%s/%s/pull_requests/%d.html" , cfg .Owner , cfg .Repo , cfg .PullRequestNumber )
84+ } else if cfg .CommitSHA != "" {
85+ pagePath = fmt .Sprintf ("/%s/%s/commits/%s.html" , cfg .Owner , cfg .Repo , cfg .CommitSHA )
86+ }
87+
88+ if err := os .RemoveAll (observe .OutputDir ); err != nil {
89+ return fmt .Errorf ("failed to clean observe output: %w" , err )
90+ }
91+ return observe .Interactive (logger , githubClient , pagePath )
8792 },
8893}
8994
9095func init () {
91- gatherCmd .Flags ().BoolVarP (& forceUpdate , "force-update" , "u" , false , "Force update of existing data" )
92- gatherCmd .Flags ().StringVarP (& owner , "owner" , "o" , "" , "Repository owner" )
93- gatherCmd .Flags ().StringVarP (& repo , "repo" , "r" , "" , "Repository name" )
94- gatherCmd .Flags ().StringVarP (& commitSHA , "commit-sha" , "c" , "" , "Commit SHA" )
95- gatherCmd .Flags ().Int64VarP (& workflowRunID , "workflow-run-id" , "w" , 0 , "Workflow run ID" )
96- gatherCmd .Flags ().IntVarP (& pullRequestNumber , "pull-request-number" , "p" , 0 , "Pull request number" )
9796 gatherCmd .Flags ().
98- StringVarP (& githubToken , "github-token" , "t" , "" , fmt .Sprintf ("GitHub API token (can also be set via %s)" , gather .GitHubTokenEnvVar ))
99-
100- if err := gatherCmd .MarkFlagRequired ("owner" ); err != nil {
101- fmt .Printf ("ERROR: Failed to mark owner flag as required: %s\n " , err .Error ())
102- os .Exit (1 )
103- }
104- if err := gatherCmd .MarkFlagRequired ("repo" ); err != nil {
105- fmt .Printf ("ERROR: Failed to mark repo flag as required: %s\n " , err .Error ())
106- os .Exit (1 )
107- }
97+ BoolVar (& noObserve , "no-observe" , false , "Skip launching the interactive observer after gathering" )
98+ gatherCmd .Flags ().BoolVarP (& forceUpdate , "force-update" , "u" , false , "Force update of existing data" )
99+ gatherCmd .Flags ().StringP ("owner" , "o" , "" , "Repository owner" )
100+ gatherCmd .Flags ().StringP ("repo" , "r" , "" , "Repository name" )
101+ gatherCmd .Flags ().StringP ("commit-sha" , "c" , "" , "Commit SHA" )
102+ gatherCmd .Flags ().Int64P ("workflow_run_id" , "w" , 0 , "Workflow run ID" )
103+ gatherCmd .Flags ().IntP ("pull_request_number" , "p" , 0 , "Pull request number" )
104+ gatherCmd .Flags ().StringP ("github_token" , "t" , "" , "GitHub API token (env: GITHUB_TOKEN)" )
108105
109106 rootCmd .AddCommand (gatherCmd )
110107}
0 commit comments