Skip to content

Commit

Permalink
Added version command. Stage from context for migration command is re…
Browse files Browse the repository at this point in the history
…ceived as Interface. Introduced CommandContextKey type for command context receiving.
  • Loading branch information
smgladkovskiy committed Nov 3, 2021
1 parent 3fa3059 commit c06ef6e
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 18 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ go 1.16
require (
github.com/jackc/pgx/v4 v4.13.0
github.com/pressly/goose v2.7.0+incompatible
github.com/spacetab-io/configuration-structs-go v0.0.0-20211027083052-7b197881fc88
github.com/spacetab-io/configuration-go v1.2.0
github.com/spacetab-io/configuration-structs-go v0.0.1
github.com/spacetab-io/logs-go/v2 v2.1.0
github.com/spf13/cobra v1.2.1
)
Expand Down
8 changes: 6 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ github.com/hashicorp/serf v0.8.2/go.mod h1:6hOLApaqBFA1NXqRQAsxw9QxuDEvNxSQRwA/J
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
github.com/imkira/go-interpol v1.1.0/go.mod h1:z0h2/2T3XF8kyEPpRgJ3kmNv+C43p+I/CoI+jC3w2iA=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
Expand Down Expand Up @@ -375,8 +376,10 @@ github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMB
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
github.com/spacetab-io/configuration-structs-go v0.0.0-20211027083052-7b197881fc88 h1:2mgKx/Q18/+TBG2cK7vuQ+Rw7sRepRwCrmcyj7/BUqo=
github.com/spacetab-io/configuration-structs-go v0.0.0-20211027083052-7b197881fc88/go.mod h1:inP70vRNRW0euxR3ndNE8/H2qXL3hmQdPkcgRfjj6AA=
github.com/spacetab-io/configuration-go v1.2.0 h1:RDoruNQfe508DpHKj5NaLdtWtCGg5e4gnhD9EOUgQ7Y=
github.com/spacetab-io/configuration-go v1.2.0/go.mod h1:242FyTS40EpL9c/zv+qjimoWh1TtAPYljZgxMsFJ4wY=
github.com/spacetab-io/configuration-structs-go v0.0.1 h1:2evzu+CH5oUJS0y6edAlJr1UTRC3oJZG8NoSPQqmI7k=
github.com/spacetab-io/configuration-structs-go v0.0.1/go.mod h1:inP70vRNRW0euxR3ndNE8/H2qXL3hmQdPkcgRfjj6AA=
github.com/spacetab-io/logs-go/v2 v2.1.0 h1:8kRAudrGlqnU8sXzauRlS6uz9UBr5K9zM8g3qjwIuk4=
github.com/spacetab-io/logs-go/v2 v2.1.0/go.mod h1:or/5vcOpnWRGA2KUJbJ6o+R2WU4XIjc4w7FDBcm7z2I=
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
Expand Down Expand Up @@ -813,6 +816,7 @@ gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20191120175047-4206685974f2/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
11 changes: 11 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package commands

type CommandContextKey string

const (
CommandContextCfgKeyOverall CommandContextKey = "cfg"
CommandContextCfgKeyDB = CommandContextCfgKeyOverall + ".db"
CommandContextCfgKeyLog = CommandContextCfgKeyOverall + ".log"
CommandContextCfgKeyAppInfo = CommandContextCfgKeyOverall + ".appInfo"
CommandContextCfgKeyStage = CommandContextCfgKeyOverall + ".stage"
)
30 changes: 15 additions & 15 deletions migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ import (
"github.com/jackc/pgx/v4/log/zerologadapter"
"github.com/jackc/pgx/v4/stdlib"
"github.com/pressly/goose"
"github.com/spacetab-io/configuration-go/stage"
cfgstructs "github.com/spacetab-io/configuration-structs-go"
log "github.com/spacetab-io/logs-go/v2"
"github.com/spf13/cobra"
)

const (
failureCode = 1
failureCode = 1
errStrFormat = "%s %s error: %w"
)

var (
Expand Down Expand Up @@ -58,18 +60,16 @@ Args:
return nil
}

const errStrFormat = "%s %s error: %w"

// migrate is a function for cobra.Command RunE param.
func migrate(cmd *cobra.Command, args []string) error {
method := "migrate"

envStage, dbCfg, logCfg, appInfo, err := getConfigs(cmd.Context())
appStage, dbCfg, logCfg, appInfo, err := getConfigs(cmd.Context())
if err != nil {
return fmt.Errorf(errStrFormat, method, "getConfig", err)
}

if err := log.Init(envStage, logCfg, appInfo.GetAlias(), appInfo.GetVersion(), os.Stdout); err != nil {
if err := log.Init(appStage.String(), logCfg, appInfo.GetAlias(), appInfo.GetVersion(), os.Stdout); err != nil {
log.Error().Err(err).Send()

return fmt.Errorf(errStrFormat, method, "log.Init", err)
Expand Down Expand Up @@ -174,31 +174,31 @@ INSERT INTO %s.%s ("version_id", "is_applied", "tstamp") VALUES ('0', 't', NOW()
}

func getConfigs(ctx context.Context) (
string,
stage.Interface,
cfgstructs.DatabaseCfgInterface,
log.Config,
cfgstructs.ApplicationInfoCfgInterface,
error,
) {
envStage, ok := ctx.Value("cfg.envStage").(string)
appStage, ok := ctx.Value(CommandContextCfgKeyStage).(stage.Interface)
if !ok {
return "", nil, log.Config{}, nil, fmt.Errorf("%w: stage name (cfg.envStage)", ErrBadContextValue)
return nil, nil, log.Config{}, nil, fmt.Errorf("%w: stage name (cfg.envStage)", ErrBadContextValue)
}

dbCfg, ok := ctx.Value("cfg.db").(cfgstructs.DatabaseCfgInterface)
dbCfg, ok := ctx.Value(CommandContextCfgKeyDB).(cfgstructs.DatabaseCfgInterface)
if !ok {
return "", nil, log.Config{}, nil, fmt.Errorf("%w: database config (cfg.db)", ErrBadContextValue)
return nil, nil, log.Config{}, nil, fmt.Errorf("%w: database config (cfg.db)", ErrBadContextValue)
}

logCfg, ok := ctx.Value("cfg.log").(log.Config)
logCfg, ok := ctx.Value(CommandContextCfgKeyLog).(log.Config)
if !ok {
return "", nil, log.Config{}, nil, fmt.Errorf("%w: log config (cfg.log)", ErrBadContextValue)
return nil, nil, log.Config{}, nil, fmt.Errorf("%w: log config (cfg.log)", ErrBadContextValue)
}

appInfo, ok := ctx.Value("cfg.appInfo").(cfgstructs.ApplicationInfoCfgInterface)
appInfoCfg, ok := ctx.Value(CommandContextCfgKeyAppInfo).(cfgstructs.ApplicationInfoCfgInterface)
if !ok {
return "", nil, log.Config{}, nil, fmt.Errorf("%w: app info config (cfg.appInfo)", ErrBadContextValue)
return nil, nil, log.Config{}, nil, fmt.Errorf("%w: app info config (cfg.appInfo)", ErrBadContextValue)
}

return envStage, dbCfg, logCfg, appInfo, nil
return appStage, dbCfg, logCfg, appInfoCfg, nil
}
23 changes: 23 additions & 0 deletions version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package commands

import (
"fmt"

cfgstructs "github.com/spacetab-io/configuration-structs-go"
"github.com/spf13/cobra"
)

var VersionCmd = &cobra.Command{
Use: "version",
Short: "Print the version number",
RunE: func(cmd *cobra.Command, args []string) error {
appInfo, ok := cmd.Context().Value(CommandContextCfgKeyAppInfo).(cfgstructs.ApplicationInfoCfgInterface)
if !ok {
return fmt.Errorf("%w: app info config (cfg.appInfo)", ErrBadContextValue)
}

cmd.Println(appInfo.GetVersion())

return nil
},
}

0 comments on commit c06ef6e

Please sign in to comment.