generated from actions/hello-world-docker-action
-
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.
- Loading branch information
Showing
13 changed files
with
829 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/neondatabase/gh-workflow-stats-action/internal/db" | ||
"github.com/neondatabase/gh-workflow-stats-action/internal/exporter" | ||
"github.com/neondatabase/gh-workflow-stats-action/internal/githubclient" | ||
) | ||
|
||
func NewExportCommand() *cobra.Command { | ||
dbCfg := &db.Config{} | ||
ghCfg := &githubclient.Config{} | ||
ghRunID := int64(0) | ||
ghRunAttempt := int64(0) | ||
|
||
exportCmd := &cobra.Command{ | ||
Use: "export", | ||
Short: "A brief description of your command", | ||
Long: `A longer description that spans multiple lines and likely contains examples | ||
and usage of using your command. For example: | ||
Cobra is a CLI library for Go that empowers applications. | ||
This application is a tool to generate the needed files | ||
to quickly create a Cobra application.`, | ||
Run: buildRunCommand(dbCfg, ghCfg, ghRunID, ghRunAttempt), | ||
} | ||
|
||
exportCmd.Flags().StringVar(&dbCfg.DSN, "db-uri", "", "DB DSN") | ||
exportCmd.Flags().StringVar(&dbCfg.TableName, "db-table", "", "DB Table") | ||
exportCmd.Flags().StringVar(&ghCfg.Repository, "github-repository", "", "Github repository") | ||
exportCmd.Flags().StringVar(&ghCfg.Token, "github-token", "", "Github token") | ||
exportCmd.Flags().Int64Var(&ghRunID, "github-run-id", 0, "Github run ID") | ||
exportCmd.Flags().Int64Var(&ghRunAttempt, "github-run-attempt", 0, "Github run ID") | ||
|
||
exportCmd.MarkFlagsRequiredTogether("github-repository", "github-run-id", "github-token", "github-run-attempt") | ||
|
||
exportCmd.MarkFlagRequired("db-uri") | ||
exportCmd.MarkFlagRequired("github-repository") | ||
|
||
return exportCmd | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(NewExportCommand()) | ||
} | ||
|
||
func buildRunCommand(dbCfg *db.Config, ghCfg *githubclient.Config, ghRunID int64, ghRunAttempt int64) func(*cobra.Command, []string) { | ||
return func(cmd *cobra.Command, args []string) { | ||
repo, err := db.NewDatabase(dbCfg) | ||
if err != nil { | ||
fmt.Println(err) | ||
return | ||
} | ||
|
||
ghClient, err := githubclient.NewClient(ghCfg) | ||
if err != nil { | ||
fmt.Println(err) | ||
return | ||
} | ||
|
||
exp := exporter.New(repo, ghClient) | ||
if err := exp.ExportByRunAttempt(context.Background(), ghRunID, ghRunAttempt); err != nil { | ||
fmt.Println(err) | ||
return | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/spf13/pflag" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
// rootCmd represents the base command when called without any subcommands | ||
var rootCmd = &cobra.Command{ | ||
Use: "gh-workflow-stats-action", | ||
Short: "A brief description of your application", | ||
Long: `A longer description that spans multiple lines and likely contains | ||
examples and usage of using your application. For example: | ||
Cobra is a CLI library for Go that empowers applications. | ||
This application is a tool to generate the needed files | ||
to quickly create a Cobra application.`, | ||
PersistentPreRunE: func(cmd *cobra.Command, args []string) error { | ||
return initializeConfig(cmd) | ||
}, | ||
// Uncomment the following line if your bare application | ||
// has an action associated with it: | ||
Run: func(cmd *cobra.Command, args []string) { | ||
fmt.Println(args) | ||
}, | ||
} | ||
|
||
// Execute adds all child commands to the root command and sets flags appropriately. | ||
// This is called by main.main(). It only needs to happen once to the rootCmd. | ||
func Execute() { | ||
err := rootCmd.Execute() | ||
if err != nil { | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
func init() { | ||
// Here you will define your flags and configuration settings. | ||
// Cobra supports persistent flags, which, if defined here, | ||
// will be global for your application. | ||
|
||
// rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.gh-workflow-stats-action.yaml)") | ||
|
||
// Cobra also supports local flags, which will only run | ||
// when this action is called directly. | ||
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") | ||
} | ||
|
||
func initializeConfig(cmd *cobra.Command) error { | ||
v := viper.New() | ||
// Environment variables can't have dashes in them, so bind them to their equivalent | ||
// keys with underscores, e.g. --favorite-color to STING_FAVORITE_COLOR | ||
v.SetEnvKeyReplacer(strings.NewReplacer("-", "_")) | ||
|
||
// Bind to environment variables | ||
// Works great for simple config names, but needs help for names | ||
// like --favorite-color which we fix in the bindFlags function | ||
v.AutomaticEnv() | ||
|
||
// Bind the current command's flags to viper | ||
bindFlags(cmd, v) | ||
|
||
return nil | ||
} | ||
|
||
// Bind each cobra flag to its associated viper configuration (config file and environment variable) | ||
func bindFlags(cmd *cobra.Command, v *viper.Viper) { | ||
cmd.Flags().VisitAll(func(f *pflag.Flag) { | ||
// Determine the naming convention of the flags when represented in the config file | ||
configName := f.Name | ||
|
||
// Apply the viper config value to the flag when the flag is not set and viper has a value | ||
if !f.Changed && v.IsSet(configName) { | ||
val := v.Get(configName) | ||
cmd.Flags().Set(f.Name, fmt.Sprintf("%v", val)) | ||
} | ||
}) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package db | ||
|
||
type Config struct { | ||
DSN string | ||
TableName string | ||
} |
Oops, something went wrong.