diff --git a/.github/workflows/pr.yaml b/.github/workflows/pr.yaml index 5dc60962..d9162af0 100644 --- a/.github/workflows/pr.yaml +++ b/.github/workflows/pr.yaml @@ -14,6 +14,9 @@ jobs: timeout-minutes: 20 env: HELM_VERSION: v2.14.3 + defaults: + run: + working-directory: ./src/github.com/${{ github.repository }} steps: - uses: actions/setup-go@v2 with: @@ -30,17 +33,24 @@ jobs: with: path: | ~/go/pkg/mod - ${{ github.workspace }}/pkg/mod key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} restore-keys: | ${{ runner.os }}-go- - run: echo ${{ github.workspace }}/bin >> $GITHUB_PATH + - run: make templating + - name: Lint Code + uses: golangci/golangci-lint-action@v2 + with: + working-directory: ./src/github.com/${{ github.repository }} - name: Test - working-directory: ./src/github.com/${{ github.repository }} env: GOPATH: ${{ github.workspace }} run: | git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com" git config --global user.name "github-actions[bot]" - make test-with-integration ${{ github.event.inputs.make-flags }} + make test-with-integration ${{ github.event.inputs.make-flags }} skip_teardown=true + - name: Lint Generated Code + uses: golangci/golangci-lint-action@v2 + with: + working-directory: ./src/github.com/${{ github.repository }}/atlas/test diff --git a/.golangci.yaml b/.golangci.yaml new file mode 100644 index 00000000..948ba0e0 --- /dev/null +++ b/.golangci.yaml @@ -0,0 +1,6 @@ +run: + skip-dirs: + - templates +linters: + enable: + - goimports diff --git a/atlas/commands/bootstrap/expand.go b/atlas/commands/bootstrap/expand.go index 9bd59fc1..1aa994ae 100644 --- a/atlas/commands/bootstrap/expand.go +++ b/atlas/commands/bootstrap/expand.go @@ -3,8 +3,6 @@ package bootstrap import ( "bufio" "fmt" - "github.com/infobloxopen/atlas-cli/atlas/templates" - "github.com/jinzhu/inflection" "io" "log" "os" @@ -13,6 +11,8 @@ import ( "text/template" "github.com/iancoleman/strcase" + "github.com/infobloxopen/atlas-cli/atlas/templates" + "github.com/jinzhu/inflection" ) type finalTemplate struct { @@ -93,7 +93,9 @@ func expandResource(appName, expandName string, withDatabase bool) error { log.Fatalf("failed to create cmd/server/servers.go\n%s\n", err) } - os.MkdirAll("db/migration", os.ModePerm) + if err := os.MkdirAll("db/migration", os.ModePerm); err != nil { + log.Fatalf("failed to create %q: %v", "db/migration", err) + } for _, res := range r { err = runTemplate([]templateResource{res}, appName, withDatabase, diff --git a/atlas/commands/command.go b/atlas/commands/command.go index 4aa56171..947b334d 100644 --- a/atlas/commands/command.go +++ b/atlas/commands/command.go @@ -2,9 +2,9 @@ package commands import ( "flag" - "github.com/infobloxopen/atlas-cli/atlas/commands/update" "github.com/infobloxopen/atlas-cli/atlas/commands/bootstrap" + "github.com/infobloxopen/atlas-cli/atlas/commands/update" ) // Command generically represents a command that is runnable via the atlas diff --git a/atlas/commands/update/update.go b/atlas/commands/update/update.go index a7f0b3a5..a51f939a 100644 --- a/atlas/commands/update/update.go +++ b/atlas/commands/update/update.go @@ -3,8 +3,9 @@ package update import ( "flag" "fmt" - "github.com/infobloxopen/atlas-cli/atlas/application" "os" + + "github.com/infobloxopen/atlas-cli/atlas/application" ) type StructureError struct { diff --git a/atlas/main.go b/atlas/main.go index 93e385f2..19290406 100644 --- a/atlas/main.go +++ b/atlas/main.go @@ -19,7 +19,10 @@ func main() { fmt.Printf("Command \"%s\" is not valid. Please choose one of %v\n", os.Args[1], commands.GetCommandNames()) os.Exit(1) } - command.GetFlagset().Parse(os.Args[2:]) + if err := command.GetFlagset().Parse(os.Args[2:]); err != nil { + fmt.Println("failed to parse flags:", err) + os.Exit(1) + } if err := command.Run(); err != nil { fmt.Println(err) os.Exit(1) diff --git a/atlas/main_test.go b/atlas/main_test.go index 42f495dc..fbff7da1 100644 --- a/atlas/main_test.go +++ b/atlas/main_test.go @@ -39,7 +39,9 @@ func TestMain(m *testing.M) { log.Print(string(out)) log.Fatalf("failed to run atlas init-app: %v", err) } - defer e2eTeardown() + if len(os.Getenv("skip_teardown")) == 0 { + defer e2eTeardown() + } ctx, cancel := context.WithCancel(context.Background()) defer cancel() @@ -88,7 +90,9 @@ func TestMain(m *testing.M) { code := m.Run() // os.Exit() does not respect defer statements - e2eTeardown() + if len(os.Getenv("skip_teardown")) == 0 { + e2eTeardown() + } os.Exit(code) } diff --git a/atlas/templates/cmd/server/config.go.gotmpl b/atlas/templates/cmd/server/config.go.gotmpl index 7b1be3ba..9f54e86b 100644 --- a/atlas/templates/cmd/server/config.go.gotmpl +++ b/atlas/templates/cmd/server/config.go.gotmpl @@ -81,65 +81,65 @@ const ( var ( // define flag overrides - flagServerAddress = pflag.String("server.address", defaultServerAddress, "adress of gRPC server") - flagServerPort = pflag.String("server.port", defaultServerPort, "port of gRPC server") - - flagGatewayEnable = pflag.Bool("gateway.enable", defaultGatewayEnable, "enable gatway") - flagGatewayAddress = pflag.String("gateway.address", defaultGatewayAddress, "address of gateway server") - flagGatewayPort = pflag.String("gateway.port", defaultGatewayPort, "port of gateway server") - flagGatewayURL = pflag.String("gateway.endpoint", defaultGatewayURL, "endpoint of gateway server") - flagGatewaySwaggerFile = pflag.String("gateway.swaggerFile", defaultGatewaySwaggerFile, "directory of swagger.json file") - - flagDatabaseEnable = pflag.Bool("database.enable", defaultDatabaseEnable, "enable database") - flagDatabaseDSN = pflag.String("database.dsn", defaultDatabaseDSN, "DSN of the database") - flagDatabaseType = pflag.String("database.type", defaultDatabaseType, "type of the database") - flagDatabaseAddress = pflag.String("database.address", defaultDatabaseAddress, "address of the database") - flagDatabasePort = pflag.String("database.port", defaultDatabasePort, "port of the database") - flagDatabaseName = pflag.String("database.name", defaultDatabaseName, "name of the database") - flagDatabaseUser = pflag.String("database.user", defaultDatabaseUser, "database username") - flagDatabasePassword = pflag.String("database.password", defaultDatabasePassword, "database password") - flagDatabaseSSL = pflag.String("database.ssl", defaultDatabaseSSL, "database ssl mode") - flagDatabaseOption = pflag.String("database.option", defaultDatabaseOption, "define custom option to db driver") - - flagPubsubEnable = pflag.Bool("atlas.pubsub.enable", defaultPubsubEnable, "enable application with pubsub") - flagPubsubAddress = pflag.String("atlas.pubsub.address", defaultPubsubAddress, "address or FQDN of the pubsub service") - flagPubsubPort = pflag.String("atlas.pubsub.port", defaultPubsubPort, "port of the pubsub service") - flagPubsubPublish = pflag.String("atlas.pubsub.publish", defaultPubsubPublish, "publisher topic") - flagPubsubSubscribe = pflag.String("atlas.pubsub.subscribe", defaultPubsubSubscribe, "subscriber topic") - flagPubsubSubscriberID = pflag.String("atlas.pubsub.subscriber.id", defaultPubsubSubscriberID, "subscriber id") - - - flagAuthzEnable = pflag.Bool("atlas.authz.enable", defaultAuthzEnable, "enable application with authorization") - flagAuthzAddress = pflag.String("atlas.authz.address", defaultAuthzAddress, "address or FQDN of the authorization service") - flagAuthzPort = pflag.String("atlas.authz.port", defaultAuthzPort, "port of the authorization service") - - flagAuditEnable = pflag.Bool("atlas.audit.enable", defaultAuditEnable, "enable logging of gRPC requests on Atlas audit service") - flagAuditAddress = pflag.String("atlas.audit.address", defaultAuditAddress, "address or FQDN of Atlas audit log service") - flagAuditPort = pflag.String("atlas.audit.port", defaultAuditPort, "port of Atlas audit log service") - - flagTaggingEnable = pflag.Bool("atlas.tagging.enable", defaultTaggingEnable, "enable tagging") - flagTaggingAddress = pflag.String("atlas.tagging.address", defaultTaggingAddress, "address or FQDN of Atlas tagging service") - flagTaggingPort = pflag.String("atlas.tagging.port", defaultTaggingPort, "port of Atlas tagging service") - - flagInternalEnable = pflag.Bool("internal.enable", defaultInternalEnable, "enable internal http server") - flagInternalAddress = pflag.String("internal.address", defaultInternalAddress, "address of internal http server") - flagInternalPort = pflag.String("internal.port", defaultInternalPort, "port of internal http server") - flagInternalHealth = pflag.String("internal.health", defaultInternalHealth, "endpoint for health checks") - flagInternalReadiness = pflag.String("internal.readiness", defaultInternalReadiness, "endpoint for readiness checks") - - flagConfigDirectory = pflag.String("config.source", defaultConfigDirectory, "directory of the configuration file") - flagConfigFile = pflag.String("config.file", defaultConfigFile, "directory of the configuration file") - flagSecretFile = pflag.String("config.secret.file", defaultSecretFile, "directory of the secrets configuration file") - flagApplicationID = pflag.String("app.id", defaultApplicationID, "identifier for the application") - - flagKeepaliveTime = pflag.Int("config.keepalive.time", defaultKeepaliveTime, "default value, in seconds, of the keepalive time") - flagKeepaliveTimeout = pflag.Int("config.keepalive.timeout", defaultKeepaliveTimeout, "default value, in seconds, of the keepalive timeout") - - flagLoggingLevel = pflag.String("logging.level", defaultLoggingLevel, "log level of application") + _ = pflag.String("server.address", defaultServerAddress, "adress of gRPC server") + _ = pflag.String("server.port", defaultServerPort, "port of gRPC server") + + _ = pflag.Bool("gateway.enable", defaultGatewayEnable, "enable gatway") + _ = pflag.String("gateway.address", defaultGatewayAddress, "address of gateway server") + _ = pflag.String("gateway.port", defaultGatewayPort, "port of gateway server") + _ = pflag.String("gateway.endpoint", defaultGatewayURL, "endpoint of gateway server") + _ = pflag.String("gateway.swaggerFile", defaultGatewaySwaggerFile, "directory of swagger.json file") + + _ = pflag.Bool("database.enable", defaultDatabaseEnable, "enable database") + _ = pflag.String("database.dsn", defaultDatabaseDSN, "DSN of the database") + _ = pflag.String("database.type", defaultDatabaseType, "type of the database") + _ = pflag.String("database.address", defaultDatabaseAddress, "address of the database") + _ = pflag.String("database.port", defaultDatabasePort, "port of the database") + _ = pflag.String("database.name", defaultDatabaseName, "name of the database") + _ = pflag.String("database.user", defaultDatabaseUser, "database username") + _ = pflag.String("database.password", defaultDatabasePassword, "database password") + _ = pflag.String("database.ssl", defaultDatabaseSSL, "database ssl mode") + _ = pflag.String("database.option", defaultDatabaseOption, "define custom option to db driver") + + _ = pflag.Bool("atlas.pubsub.enable", defaultPubsubEnable, "enable application with pubsub") + _ = pflag.String("atlas.pubsub.address", defaultPubsubAddress, "address or FQDN of the pubsub service") + _ = pflag.String("atlas.pubsub.port", defaultPubsubPort, "port of the pubsub service") + _ = pflag.String("atlas.pubsub.publish", defaultPubsubPublish, "publisher topic") + _ = pflag.String("atlas.pubsub.subscribe", defaultPubsubSubscribe, "subscriber topic") + _ = pflag.String("atlas.pubsub.subscriber.id", defaultPubsubSubscriberID, "subscriber id") + + + _ = pflag.Bool("atlas.authz.enable", defaultAuthzEnable, "enable application with authorization") + _ = pflag.String("atlas.authz.address", defaultAuthzAddress, "address or FQDN of the authorization service") + _ = pflag.String("atlas.authz.port", defaultAuthzPort, "port of the authorization service") + + _ = pflag.Bool("atlas.audit.enable", defaultAuditEnable, "enable logging of gRPC requests on Atlas audit service") + _ = pflag.String("atlas.audit.address", defaultAuditAddress, "address or FQDN of Atlas audit log service") + _ = pflag.String("atlas.audit.port", defaultAuditPort, "port of Atlas audit log service") + + _ = pflag.Bool("atlas.tagging.enable", defaultTaggingEnable, "enable tagging") + _ = pflag.String("atlas.tagging.address", defaultTaggingAddress, "address or FQDN of Atlas tagging service") + _ = pflag.String("atlas.tagging.port", defaultTaggingPort, "port of Atlas tagging service") + + _ = pflag.Bool("internal.enable", defaultInternalEnable, "enable internal http server") + _ = pflag.String("internal.address", defaultInternalAddress, "address of internal http server") + _ = pflag.String("internal.port", defaultInternalPort, "port of internal http server") + _ = pflag.String("internal.health", defaultInternalHealth, "endpoint for health checks") + _ = pflag.String("internal.readiness", defaultInternalReadiness, "endpoint for readiness checks") + + _ = pflag.String("config.source", defaultConfigDirectory, "directory of the configuration file") + _ = pflag.String("config.file", defaultConfigFile, "directory of the configuration file") + _ = pflag.String("config.secret.file", defaultSecretFile, "directory of the secrets configuration file") + _ = pflag.String("app.id", defaultApplicationID, "identifier for the application") + + _ = pflag.Int("config.keepalive.time", defaultKeepaliveTime, "default value, in seconds, of the keepalive time") + _ = pflag.Int("config.keepalive.timeout", defaultKeepaliveTimeout, "default value, in seconds, of the keepalive timeout") + + _ = pflag.String("logging.level", defaultLoggingLevel, "log level of application") {{ if .WithProfiler }} - flagProfilerEnabled = pflag.Bool("profiler.enable", defaultProfilerEnabled, "enable profiler by default false") - flagProfilerAddress = pflag.String("profiler.address", defaultProfilerAddress, "address of profiler server") - flagProfilerPort = pflag.String("profiler.port", defaultProfilerPort, "port of profiler server") + _ = pflag.Bool("profiler.enable", defaultProfilerEnabled, "enable profiler by default false") + _ = pflag.String("profiler.address", defaultProfilerAddress, "address of profiler server") + _ = pflag.String("profiler.port", defaultProfilerPort, "port of profiler server") {{ end }} ) diff --git a/atlas/templates/cmd/server/main.go.gotmpl b/atlas/templates/cmd/server/main.go.gotmpl index a6a9f254..4b92b799 100644 --- a/atlas/templates/cmd/server/main.go.gotmpl +++ b/atlas/templates/cmd/server/main.go.gotmpl @@ -166,7 +166,9 @@ func ServeExternal(logger *logrus.Logger) error { func init() { pflag.Parse() - viper.BindPFlags(pflag.CommandLine) + if err := viper.BindPFlags(pflag.CommandLine); err != nil { + log.Fatal(err) + } viper.AutomaticEnv() viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) viper.AddConfigPath(viper.GetString("config.source")) diff --git a/atlas/templates/pkg/svc/zserver_test.go.gotmpl b/atlas/templates/pkg/svc/zserver_test.go.gotmpl index 71b7c8fa..2bb8c425 100644 --- a/atlas/templates/pkg/svc/zserver_test.go.gotmpl +++ b/atlas/templates/pkg/svc/zserver_test.go.gotmpl @@ -26,7 +26,7 @@ func TestGetVersion(t *testing.T) { err, test.err, ) } - res, err := server.GetVersion(context.Background(), &empty.Empty{}) + res, _ := server.GetVersion(context.Background(), &empty.Empty{}) if res.Version != test.expected.Version { t.Errorf("Unexpected version in response: %v - expected: %v", res.Version, test.expected.Version,