Merge pull request #114 from foomo/feature/refactor-cli

refactor: cli setup
This commit is contained in:
Kevin Franklin Kim 2025-03-27 21:25:41 +01:00 committed by GitHub
commit ad1fa735e0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
19 changed files with 294 additions and 231 deletions

View File

@ -26,7 +26,7 @@ jobs:
with:
token: ${{ secrets.GITHUB_TOKEN }}
- uses: golangci/golangci-lint-action@v6
- uses: golangci/golangci-lint-action@v7
with:
version: latest

View File

@ -1,108 +1,14 @@
# yaml-language-server: $schema=https://golangci-lint.run/jsonschema/golangci.jsonschema.json
# https://golangci-lint.run/usage/configuration/
version: "2"
run:
go: 1.24.1
build-tags: [safe]
build-tags:
- safe
modules-download-mode: readonly
issues:
exclude-dirs:
- 'bin'
- 'tmp'
exclude-rules:
- path: _test\.go
linters:
- forbidigo
- forcetypeassert
linters-settings:
# https://golangci-lint.run/usage/linters/#misspell
misspell:
mode: restricted
# https://golangci-lint.run/usage/linters/#asasalint
asasalint:
ignore-test: true
# https://golangci-lint.run/usage/linters/#exhaustive
exhaustive:
default-signifies-exhaustive: true
# https://golangci-lint.run/usage/linters/#predeclared
predeclared:
ignore: "new,error"
# https://golangci-lint.run/usage/linters/#gocritic
gocritic:
disabled-checks:
- ifElseChain
- commentFormatting
# https://golangci-lint.run/usage/linters/#testifylint
testifylint:
disable:
- float-compare
# https://golangci-lint.run/usage/linters/#gosec
gosec:
confidence: medium
# https://golangci-lint.run/usage/linters/#importas
importas:
no-unaliased: true
# https://golangci-lint.run/usage/linters/#gomoddirectives
gomoddirectives:
replace-local: true
# https://golangci-lint.run/usage/linters/#revive
revive:
ignore-generated-header: true
enable-all-rules: true
rules:
- name: line-length-limit
disabled: true
- name: cognitive-complexity
disabled: true
- name: unused-parameter
disabled: true
- name: add-constant
disabled: true
- name: cyclomatic
disabled: true
- name: function-length
disabled: true
- name: function-result-limit
disabled: true
- name: flag-parameter
disabled: true
- name: unused-receiver
disabled: true
- name: argument-limit
disabled: true
- name: max-control-nesting
disabled: true
- name: comment-spacings
disabled: true
- name: max-public-structs
disabled: true
- name: struct-tag
arguments:
- "json,inline"
- "yaml,squash"
- name: unhandled-error
arguments:
- "fmt.Println"
- "viper.BindPFlag"
- "strings.Builder.WriteString"
# TODO remove
- name: deep-exit
disabled: true
- name: if-return
disabled: true
- name: empty-block
disabled: true
- name: indent-error-flow
disabled: true
linters:
disable-all: true
default: none
enable:
## Default linters
- errcheck # errcheck is a program for checking for unchecked errors in Go code. These unchecked errors can be critical bugs in some cases [fast: false, auto-fix: false]
- gosimple # (megacheck) Linter for Go source code that specializes in simplifying code [fast: false, auto-fix: false]
- govet # (vet, vetshadow) Vet examines Go source code and reports suspicious constructs. It is roughly the same as 'go vet' and uses its passes. [fast: false, auto-fix: false]
- ineffassign # Detects when assignments to existing variables are not used [fast: true, auto-fix: false]
- staticcheck # (megacheck) It's a set of rules from staticcheck. It's not the same thing as the staticcheck binary. The author of staticcheck doesn't support or approve the use of staticcheck as a library inside golangci-lint. [fast: false, auto-fix: false]
@ -131,9 +37,7 @@ linters:
- gochecksumtype # Run exhaustiveness checks on Go "sum types" [fast: false, auto-fix: false]
- goconst # Finds repeated strings that could be replaced by a constant [fast: true, auto-fix: false]
- gocritic # Provides diagnostics that check for bugs, performance and style issues. [fast: false, auto-fix: true]
- gofmt # Gofmt checks whether code was gofmt-ed. By default this tool runs with -s option to check for code simplification [fast: true, auto-fix: true]
- goheader # Checks is file header matches to pattern [fast: true, auto-fix: true]
- goimports # Check import statements are formatted according to the 'goimport' command. Reformat imports in autofix mode. [fast: true, auto-fix: true]
- gomoddirectives # Manage the use of 'replace', 'retract', and 'excludes' directives in go.mod. [fast: true, auto-fix: false]
- gomodguard # Allow and block list linter for direct Go module dependencies. This is different from depguard where there are different block types for example version constraints and module recommendations. [fast: true, auto-fix: false]
- goprintffuncname # Checks that printf-like functions are named with `f` at the end. [fast: true, auto-fix: false]
@ -166,7 +70,6 @@ linters:
- rowserrcheck # checks whether Rows.Err of rows is checked successfully [fast: false, auto-fix: false]
- spancheck # Checks for mistakes with OpenTelemetry/Census spans. [fast: false, auto-fix: false]
- sqlclosecheck # Checks that sql.Rows, sql.Stmt, sqlx.NamedStmt, pgx.Query are closed. [fast: false, auto-fix: false]
- stylecheck # Stylecheck is a replacement for golint [fast: false, auto-fix: false]
- testableexamples # linter checks if examples are testable (have an expected output) [fast: true, auto-fix: false]
- testifylint # Checks usage of github.com/stretchr/testify. [fast: false, auto-fix: false]
- testpackage # linter that makes you use a separate _test package [fast: true, auto-fix: false]
@ -188,7 +91,6 @@ linters:
#- err113 # Go linter to check the errors handling expressions [fast: false, auto-fix: false]
#- exhaustruct # Checks if all structure fields are initialized [fast: false, auto-fix: false]
#- funlen # Tool for detection of long functions [fast: true, auto-fix: false]
#- gci # Gci controls Go package import order and makes it always deterministic. [fast: true, auto-fix: true]
#- ginkgolinter # enforces standards of using ginkgo and gomega [fast: false, auto-fix: false]
#- gochecknoglobals # Check that no global variables exist. [fast: false, auto-fix: false]
#- gochecknoinits # Checks that no init functions are present in Go code [fast: true, auto-fix: false]
@ -196,7 +98,6 @@ linters:
#- gocyclo # Computes and checks the cyclomatic complexity of functions [fast: true, auto-fix: false]
#- godot # Check if comments end in a period [fast: true, auto-fix: true]
#- godox # Tool for detection of comment keywords [fast: true, auto-fix: false]
#- gofumpt # Gofumpt checks whether code was gofumpt-ed. [fast: true, auto-fix: true]
#- interfacebloat # A linter that checks the number of methods inside an interface. [fast: true, auto-fix: false]
#- intrange # (go >= 1.22) intrange is a linter to find places where for loops could make use of an integer range. [fast: true, auto-fix: false]
#- ireturn # Accept Interfaces, Return Concrete Types [fast: false, auto-fix: false]
@ -216,3 +117,104 @@ linters:
#- wrapcheck # Checks that errors returned from external packages are wrapped [fast: false, auto-fix: false]
#- wsl # add or remove empty lines [fast: true, auto-fix: false]
#- zerologlint # Detects the wrong usage of `zerolog` that a user forgets to dispatch with `Send` or `Msg` [fast: false, auto-fix: false]
settings:
exhaustive:
default-signifies-exhaustive: true
gocritic:
disabled-checks:
- ifElseChain
- commentFormatting
gomoddirectives:
replace-local: true
gosec:
confidence: medium
importas:
no-unaliased: true
misspell:
mode: restricted
predeclared:
ignore:
- new
- error
revive:
enable-all-rules: true
rules:
- name: line-length-limit
disabled: true
- name: cognitive-complexity
disabled: true
- name: unused-parameter
disabled: true
- name: add-constant
disabled: true
- name: cyclomatic
disabled: true
- name: function-length
disabled: true
- name: function-result-limit
disabled: true
- name: flag-parameter
disabled: true
- name: unused-receiver
disabled: true
- name: argument-limit
disabled: true
- name: max-control-nesting
disabled: true
- name: comment-spacings
disabled: true
- name: max-public-structs
disabled: true
- name: struct-tag
arguments:
- json,inline
- yaml,squash
- name: unhandled-error
arguments:
- fmt.Println
- viper.BindPFlag
- strings.Builder.WriteString
- name: deep-exit
disabled: true
- name: if-return
disabled: true
- name: empty-block
disabled: true
- name: indent-error-flow
disabled: true
testifylint:
disable:
- float-compare
exclusions:
generated: lax
presets:
- comments
- common-false-positives
- legacy
- std-error-handling
rules:
- linters:
- forbidigo
- forcetypeassert
path: _test\.go
- linters:
- asasalint
path: (.+)_test\.go
paths:
- bin
- tmp
- third_party$
- builtin$
- examples$
formatters:
enable:
- gofmt
- goimports
exclusions:
generated: lax
paths:
- bin
- tmp
- third_party$
- builtin$
- examples$

View File

@ -1,6 +1,6 @@
hooks:
pre-commit:
- golangci-lint run --fast
- golangci-lint run --fast-only
- husky lint-staged
commit-msg:
# only execute if not in a merge
@ -8,7 +8,7 @@ hooks:
lint-staged:
'*.go':
- goimports -l -w
- golangci-lint fmt
lint-commit:
types: '^(feat|fix|build|chore|docs|perf|refactor|revert|style|test|wip)$'

View File

@ -3,6 +3,7 @@ package cmd
import (
"bytes"
"encoding/json"
"log/slog"
"os"
"github.com/alecthomas/chroma/quick"
@ -10,16 +11,17 @@ import (
"github.com/itchyny/json2yaml"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
func NewConfig(root *cobra.Command) {
func NewConfig(l *slog.Logger) *cobra.Command {
c := viper.New()
cmd := &cobra.Command{
Use: "config",
Short: "Print config",
RunE: func(cmd *cobra.Command, args []string) error {
l := pkgcmd.Logger()
cfg, err := pkgcmd.ReadConfig(l, cmd)
cfg, err := pkgcmd.ReadConfig(l, c, cmd)
if err != nil {
return err
}
@ -38,5 +40,10 @@ func NewConfig(root *cobra.Command) {
},
}
root.AddCommand(cmd)
flags := cmd.Flags()
flags.StringSliceP("config", "c", []string{"sesamy.yaml"}, "config files (default is sesamy.yaml)")
_ = c.BindPFlag("config", flags.Lookup("config"))
return cmd
}

View File

@ -1,20 +1,23 @@
package cmd
import (
"log/slog"
"github.com/foomo/sesamy-cli/cmd/list"
"github.com/spf13/cobra"
)
// NewList represents the list command
func NewList(root *cobra.Command) *cobra.Command {
func NewList(l *slog.Logger) *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Short: "List Google Tag Manager containers",
}
list.NewServer(cmd)
list.NewWeb(cmd)
root.AddCommand(cmd)
cmd.AddCommand(
list.NewWeb(l),
list.NewServer(l),
)
return cmd
}

View File

@ -1,13 +1,18 @@
package list
import (
"log/slog"
pkgcmd "github.com/foomo/sesamy-cli/pkg/cmd"
"github.com/foomo/sesamy-cli/pkg/tagmanager"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
// NewServer represents the server command
func NewServer(root *cobra.Command) {
func NewServer(l *slog.Logger) *cobra.Command {
c := viper.New()
cmd := &cobra.Command{
Use: "server",
Short: "List Google Tag Manager Server Container",
@ -30,10 +35,9 @@ func NewServer(root *cobra.Command) {
},
RunE: func(cmd *cobra.Command, args []string) error {
resource := args[0]
l := pkgcmd.Logger()
l.Info("☕ Listing Server Container resources: " + resource)
cfg, err := pkgcmd.ReadConfig(l, cmd)
cfg, err := pkgcmd.ReadConfig(l, c, cmd)
if err != nil {
return err
}
@ -58,5 +62,10 @@ func NewServer(root *cobra.Command) {
},
}
root.AddCommand(cmd)
flags := cmd.Flags()
flags.StringSliceP("config", "c", []string{"sesamy.yaml"}, "config files (default is sesamy.yaml)")
_ = c.BindPFlag("config", flags.Lookup("config"))
return cmd
}

View File

@ -1,13 +1,18 @@
package list
import (
"log/slog"
pkgcmd "github.com/foomo/sesamy-cli/pkg/cmd"
"github.com/foomo/sesamy-cli/pkg/tagmanager"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
// NewWeb represents the web command
func NewWeb(root *cobra.Command) {
func NewWeb(l *slog.Logger) *cobra.Command {
c := viper.New()
cmd := &cobra.Command{
Use: "web",
Short: "List Google Tag Manager Web Container",
@ -29,10 +34,9 @@ func NewWeb(root *cobra.Command) {
},
RunE: func(cmd *cobra.Command, args []string) error {
resource := args[0]
l := pkgcmd.Logger()
l.Info("☕ Listing Web Container resources: " + resource)
cfg, err := pkgcmd.ReadConfig(l, cmd)
cfg, err := pkgcmd.ReadConfig(l, c, cmd)
if err != nil {
return err
}
@ -57,5 +61,10 @@ func NewWeb(root *cobra.Command) {
},
}
root.AddCommand(cmd)
flags := cmd.Flags()
flags.StringSliceP("config", "c", []string{"sesamy.yaml"}, "config files (default is sesamy.yaml)")
_ = c.BindPFlag("config", flags.Lookup("config"))
return cmd
}

View File

@ -1,20 +1,23 @@
package cmd
import (
"log/slog"
"github.com/foomo/sesamy-cli/cmd/provision"
"github.com/spf13/cobra"
)
// NewProvision represents the provision command
func NewProvision(root *cobra.Command) *cobra.Command {
func NewProvision(l *slog.Logger) *cobra.Command {
cmd := &cobra.Command{
Use: "provision",
Short: "Provision Google Tag Manager containers",
}
provision.NewServer(cmd)
provision.NewWeb(cmd)
root.AddCommand(cmd)
cmd.AddCommand(
provision.NewServer(l),
provision.NewWeb(l),
)
return cmd
}

View File

@ -1,6 +1,8 @@
package provision
import (
"log/slog"
pkgcmd "github.com/foomo/sesamy-cli/pkg/cmd"
conversionlinkerprovider "github.com/foomo/sesamy-cli/pkg/provider/conversionlinker"
criteoprovider "github.com/foomo/sesamy-cli/pkg/provider/criteo"
@ -22,12 +24,14 @@ import (
)
// NewServer represents the server command
func NewServer(root *cobra.Command) {
func NewServer(l *slog.Logger) *cobra.Command {
c := viper.New()
cmd := &cobra.Command{
Use: "server",
Short: "Provision Google Tag Manager Server Container",
RunE: func(cmd *cobra.Command, args []string) error {
l := pkgcmd.Logger()
l := pkgcmd.NewLogger()
l.Info("☕ Provisioning Server Container")
tags, err := cmd.Flags().GetStringSlice("tags")
@ -35,7 +39,7 @@ func NewServer(root *cobra.Command) {
return errors.Wrap(err, "error reading tags flag")
}
cfg, err := pkgcmd.ReadConfig(l, cmd)
cfg, err := pkgcmd.ReadConfig(l, c, cmd)
if err != nil {
return err
}
@ -56,75 +60,75 @@ func NewServer(root *cobra.Command) {
return err
}
if pkgcmd.Tag(googletagprovider.Tag, tags) {
if utils.Tag(googletagprovider.Tag, tags) {
if err := googletagprovider.Server(cmd.Context(), tm, cfg.GoogleTag); err != nil {
return errors.Wrap(err, "failed to provision google tag provider")
}
}
if pkgcmd.Tag(googletagmanagerprovider.Tag, tags) {
if utils.Tag(googletagmanagerprovider.Tag, tags) {
if err := googletagmanagerprovider.Server(cmd.Context(), tm, cfg.GoogleTagManager, cfg.EnableGeoResolution); err != nil {
return errors.Wrap(err, "failed to provision google tag manager")
}
}
if cfg.GoogleAnalytics.Enabled && pkgcmd.Tag(googleanalyticsprovider.Tag, tags) {
if cfg.GoogleAnalytics.Enabled && utils.Tag(googleanalyticsprovider.Tag, tags) {
l.Info("🅿️ Running provider", "name", googleanalyticsprovider.Name, "tag", googleanalyticsprovider.Tag)
if err := googleanalyticsprovider.Server(cmd.Context(), tm, cfg.GoogleAnalytics, cfg.RedactVisitorIP, cfg.EnableGeoResolution); err != nil {
return errors.Wrap(err, "failed to provision google analytics")
}
}
if cfg.ConversionLinker.Enabled && pkgcmd.Tag(conversionlinkerprovider.Tag, tags) {
if cfg.ConversionLinker.Enabled && utils.Tag(conversionlinkerprovider.Tag, tags) {
l.Info("🅿️ Running provider", "name", conversionlinkerprovider.Name, "tag", conversionlinkerprovider.Tag)
if err := conversionlinkerprovider.Server(cmd.Context(), tm, cfg.ConversionLinker); err != nil {
return errors.Wrap(err, "failed to provision conversion linker")
}
}
if cfg.Umami.Enabled && pkgcmd.Tag(umamiprovider.Tag, tags) {
if cfg.Umami.Enabled && utils.Tag(umamiprovider.Tag, tags) {
l.Info("🅿️ Running provider", "name", umamiprovider.Name, "tag", umamiprovider.Tag)
if err := umamiprovider.Server(cmd.Context(), tm, cfg.Umami); err != nil {
return errors.Wrap(err, "failed to provision umami")
}
}
if cfg.Facebook.Enabled && pkgcmd.Tag(facebookprovider.Tag, tags) {
if cfg.Facebook.Enabled && utils.Tag(facebookprovider.Tag, tags) {
l.Info("🅿️ Running provider", "name", facebookprovider.Name, "tag", facebookprovider.Tag)
if err := facebookprovider.Server(cmd.Context(), l, tm, cfg.Facebook); err != nil {
return errors.Wrap(err, "failed to provision facebook")
}
}
if cfg.GoogleAds.Enabled && pkgcmd.Tag(googleadsprovider.Tag, tags) {
if cfg.GoogleAds.Enabled && utils.Tag(googleadsprovider.Tag, tags) {
l.Info("🅿️ Running provider", "name", googleadsprovider.Name, "tag", googleadsprovider.Tag)
if err := googleadsprovider.Server(cmd.Context(), l, tm, cfg.GoogleAds); err != nil {
return errors.Wrap(err, "failed to provision google ads")
}
}
if cfg.Emarsys.Enabled && pkgcmd.Tag(emarsysprovider.Tag, tags) {
if cfg.Emarsys.Enabled && utils.Tag(emarsysprovider.Tag, tags) {
l.Info("🅿️ Running provider", "name", emarsysprovider.Name, "tag", emarsysprovider.Tag)
if err := emarsysprovider.Server(cmd.Context(), l, tm, cfg.Emarsys); err != nil {
return errors.Wrap(err, "failed to provision emarsys")
}
}
if cfg.Tracify.Enabled && pkgcmd.Tag(tracifyprovider.Tag, tags) {
if cfg.Tracify.Enabled && utils.Tag(tracifyprovider.Tag, tags) {
l.Info("🅿️ Running provider", "name", tracifyprovider.Name, "tag", tracifyprovider.Tag)
if err := tracifyprovider.Server(cmd.Context(), l, tm, cfg.Tracify); err != nil {
return errors.Wrap(err, "failed to provision tracify")
}
}
if cfg.Criteo.Enabled && pkgcmd.Tag(criteoprovider.Tag, tags) {
if cfg.Criteo.Enabled && utils.Tag(criteoprovider.Tag, tags) {
l.Info("🅿️ Running provider", "name", criteoprovider.Name, "tag", criteoprovider.Tag)
if err := criteoprovider.Server(cmd.Context(), l, tm, cfg.Criteo); err != nil {
return errors.Wrap(err, "failed to provision criteo")
}
}
if cfg.MicrosoftAds.Enabled && pkgcmd.Tag(microsoftadsprovider.Tag, tags) {
if cfg.MicrosoftAds.Enabled && utils.Tag(microsoftadsprovider.Tag, tags) {
l.Info("🅿️ Running provider", "name", microsoftadsprovider.Name, "tag", microsoftadsprovider.Tag)
if err := microsoftadsprovider.Server(cmd.Context(), l, tm, cfg.MicrosoftAds); err != nil {
return errors.Wrap(err, "failed to provision microsoftads")
@ -154,8 +158,13 @@ func NewServer(root *cobra.Command) {
},
}
cmd.Flags().StringSlice("tags", nil, "list of tags to provision")
_ = viper.BindPFlag("tags", cmd.Flags().Lookup("tags"))
flags := cmd.Flags()
root.AddCommand(cmd)
flags.StringSliceP("config", "c", []string{"sesamy.yaml"}, "config files (default is sesamy.yaml)")
_ = c.BindPFlag("config", flags.Lookup("config"))
flags.StringSlice("tags", nil, "list of tags to provision")
_ = c.BindPFlag("tags", flags.Lookup("tags"))
return cmd
}

View File

@ -1,6 +1,8 @@
package provision
import (
"log/slog"
pkgcmd "github.com/foomo/sesamy-cli/pkg/cmd"
cookiebotprovider "github.com/foomo/sesamy-cli/pkg/provider/cookiebot"
criteoprovider "github.com/foomo/sesamy-cli/pkg/provider/criteo"
@ -18,12 +20,14 @@ import (
)
// NewWeb represents the web command
func NewWeb(root *cobra.Command) {
func NewWeb(l *slog.Logger) *cobra.Command {
c := viper.New()
cmd := &cobra.Command{
Use: "web",
Short: "Provision Google Tag Manager Web Container",
RunE: func(cmd *cobra.Command, args []string) error {
l := pkgcmd.Logger()
l := pkgcmd.NewLogger()
l.Info("☕ Provisioning Web Container")
tags, err := cmd.Flags().GetStringSlice("tags")
@ -31,7 +35,7 @@ func NewWeb(root *cobra.Command) {
return errors.Wrap(err, "error reading tags flag")
}
cfg, err := pkgcmd.ReadConfig(l, cmd)
cfg, err := pkgcmd.ReadConfig(l, c, cmd)
if err != nil {
return err
}
@ -52,48 +56,48 @@ func NewWeb(root *cobra.Command) {
return err
}
if pkgcmd.Tag(googletagprovider.Tag, tags) {
if utils.Tag(googletagprovider.Tag, tags) {
l.Info("🅿️ Running provider", "name", googletagprovider.Name, "tag", googletagprovider.Tag)
if err := googletagprovider.Web(cmd.Context(), tm, cfg.GoogleTag); err != nil {
return errors.Wrap(err, "failed to provision google tag provider")
}
}
if pkgcmd.Tag(googletagmanagerprovider.Tag, tags) {
if utils.Tag(googletagmanagerprovider.Tag, tags) {
if err := googletagmanagerprovider.Web(cmd.Context(), tm, cfg.GoogleTagManager); err != nil {
return errors.Wrap(err, "failed to provision google tag manager")
}
}
if cfg.GoogleAnalytics.Enabled && pkgcmd.Tag(googleanaylticsprovider.Tag, tags) {
if cfg.GoogleAnalytics.Enabled && utils.Tag(googleanaylticsprovider.Tag, tags) {
l.Info("🅿️ Running provider", "name", googleanaylticsprovider.Name, "tag", googleanaylticsprovider.Tag)
if err := googleanaylticsprovider.Web(cmd.Context(), tm, cfg.GoogleAnalytics); err != nil {
return errors.Wrap(err, "failed to provision google analytics provider")
}
}
if cfg.Emarsys.Enabled && pkgcmd.Tag(emarsysprovider.Tag, tags) {
if cfg.Emarsys.Enabled && utils.Tag(emarsysprovider.Tag, tags) {
l.Info("🅿️ Running provider", "name", emarsysprovider.Name, "tag", emarsysprovider.Tag)
if err := emarsysprovider.Web(cmd.Context(), tm, cfg.Emarsys); err != nil {
return errors.Wrap(err, "failed to provision emarsys provider")
}
}
if cfg.Hotjar.Enabled && pkgcmd.Tag(hotjarprovider.Tag, tags) {
if cfg.Hotjar.Enabled && utils.Tag(hotjarprovider.Tag, tags) {
l.Info("🅿️ Running provider", "name", hotjarprovider.Name, "tag", hotjarprovider.Tag)
if err := hotjarprovider.Web(cmd.Context(), tm, cfg.Hotjar); err != nil {
return errors.Wrap(err, "failed to provision hotjar provider")
}
}
if cfg.Criteo.Enabled && pkgcmd.Tag(criteoprovider.Tag, tags) {
if cfg.Criteo.Enabled && utils.Tag(criteoprovider.Tag, tags) {
l.Info("🅿️ Running provider", "name", criteoprovider.Name, "tag", criteoprovider.Tag)
if err := criteoprovider.Web(cmd.Context(), l, tm, cfg.Criteo); err != nil {
return errors.Wrap(err, "failed to provision criteo provider")
}
}
if cfg.Cookiebot.Enabled && pkgcmd.Tag(cookiebotprovider.Tag, tags) {
if cfg.Cookiebot.Enabled && utils.Tag(cookiebotprovider.Tag, tags) {
l.Info("🅿️ Running provider", "name", cookiebotprovider.Name, "tag", cookiebotprovider.Tag)
if err := cookiebotprovider.Web(cmd.Context(), tm, cfg.Cookiebot); err != nil {
return errors.Wrap(err, "failed to provision cookiebot provider")
@ -122,8 +126,13 @@ func NewWeb(root *cobra.Command) {
},
}
cmd.Flags().StringSlice("tags", nil, "list of tags to provision")
_ = viper.BindPFlag("tags", cmd.Flags().Lookup("tags"))
flags := cmd.Flags()
root.AddCommand(cmd)
flags.StringSliceP("config", "c", []string{"sesamy.yaml"}, "config files (default is sesamy.yaml)")
_ = c.BindPFlag("config", flags.Lookup("config"))
flags.StringSlice("tags", nil, "list of tags to provision")
_ = c.BindPFlag("tags", flags.Lookup("tags"))
return cmd
}

View File

@ -2,11 +2,13 @@ package cmd
import (
"fmt"
"log/slog"
"os"
"runtime/debug"
"strings"
cowsay "github.com/Code-Hex/Neo-cowsay/v2"
cmdx "github.com/foomo/sesamy-cli/pkg/cmd"
"github.com/pkg/errors"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
@ -14,60 +16,44 @@ import (
)
var (
l *slog.Logger
root *cobra.Command
)
func init() {
pterm.Info.Prefix.Text = "⎈"
pterm.Info.Scope.Style = &pterm.ThemeDefault.DebugMessageStyle
pterm.Debug.Prefix.Text = "⛏︎"
pterm.Debug.Scope.Style = &pterm.ThemeDefault.DebugMessageStyle
pterm.Fatal.Prefix.Text = "⛔︎"
pterm.Fatal.Scope.Style = &pterm.ThemeDefault.DebugMessageStyle
pterm.Error.Prefix.Text = "⛌"
pterm.Error.Scope.Style = &pterm.ThemeDefault.DebugMessageStyle
pterm.Warning.Prefix.Text = "⚠"
pterm.Warning.Scope.Style = &pterm.ThemeDefault.DebugMessageStyle
pterm.Success.Prefix.Text = "✓"
pterm.Success.Scope.Style = &pterm.ThemeDefault.DebugMessageStyle
l = cmdx.NewLogger()
if scope := os.Getenv("SESAMY_SCOPE"); scope != "" {
pterm.Info.Scope.Text = scope
pterm.Debug.Scope.Text = scope
pterm.Fatal.Scope.Text = scope
pterm.Error.Scope.Text = scope
pterm.Warning.Scope.Text = scope
pterm.Success.Scope.Text = scope
}
root = NewRoot()
NewConfig(root)
NewVersion(root)
NewTags(root)
NewList(root)
NewProvision(root)
NewTypeScript(root)
root = NewRoot(l)
root.AddCommand(
NewConfig(l),
NewConfig(l),
NewList(l),
NewProvision(l),
NewTags(l),
NewTypeScript(l),
NewVersion(l),
)
}
// NewRoot represents the base command when called without any subcommands
func NewRoot() *cobra.Command {
func NewRoot(l *slog.Logger) *cobra.Command {
c := viper.New()
cmd := &cobra.Command{
Use: "sesamy",
Short: "Server Side Tag Management System",
SilenceErrors: true,
SilenceUsage: true,
PersistentPostRunE: func(cmd *cobra.Command, args []string) error {
if viper.GetBool("verbose") {
pterm.EnableDebugMessages()
}
return nil
PersistentPreRun: func(cmd *cobra.Command, args []string) {
pterm.PrintDebugMessages = viper.GetBool("verbose")
},
}
cmd.PersistentFlags().BoolP("verbose", "v", false, "output debug information")
_ = viper.BindPFlag("verbose", cmd.PersistentFlags().Lookup("verbose"))
cmd.PersistentFlags().StringSliceP("config", "c", []string{"sesamy.yaml"}, "config files (default is sesamy.yaml)")
_ = viper.BindPFlag("config", cmd.PersistentFlags().Lookup("config"))
flags := cmd.PersistentFlags()
flags.BoolP("verbose", "v", false, "output debug information")
_ = c.BindPFlag("verbose", flags.Lookup("verbose"))
return cmd
}

View File

@ -1,6 +1,8 @@
package cmd
import (
"log/slog"
"github.com/foomo/sesamy-cli/pkg/provider/conversionlinker"
"github.com/foomo/sesamy-cli/pkg/provider/cookiebot"
"github.com/foomo/sesamy-cli/pkg/provider/criteo"
@ -20,7 +22,7 @@ import (
)
// NewTags represents the tags command
func NewTags(root *cobra.Command) *cobra.Command {
func NewTags(l *slog.Logger) *cobra.Command {
cmd := &cobra.Command{
Use: "tags",
Short: "Print out all available tags",
@ -48,7 +50,5 @@ func NewTags(root *cobra.Command) *cobra.Command {
},
}
root.AddCommand(cmd)
return cmd
}

View File

@ -1,6 +1,7 @@
package cmd
import (
"log/slog"
"maps"
"os"
"path"
@ -12,17 +13,20 @@ import (
"github.com/foomo/sesamy-cli/pkg/typescript/generator"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
// NewTypeScript represents the typescript command
func NewTypeScript(root *cobra.Command) *cobra.Command {
func NewTypeScript(l *slog.Logger) *cobra.Command {
c := viper.New()
cmd := &cobra.Command{
Use: "typescript",
Short: "Generate typescript events",
RunE: func(cmd *cobra.Command, args []string) error {
l := pkgcmd.Logger()
l := pkgcmd.NewLogger()
cfg, err := pkgcmd.ReadConfig(l, cmd)
cfg, err := pkgcmd.ReadConfig(l, c, cmd)
if err != nil {
return err
}
@ -57,7 +61,10 @@ func NewTypeScript(root *cobra.Command) *cobra.Command {
},
}
root.AddCommand(cmd)
flags := cmd.Flags()
flags.StringSliceP("config", "c", []string{"sesamy.yaml"}, "config files (default is sesamy.yaml)")
_ = c.BindPFlag("config", flags.Lookup("config"))
return cmd
}

View File

@ -2,13 +2,14 @@ package cmd
import (
"fmt"
"log/slog"
"github.com/spf13/cobra"
)
var version = "latest"
func NewVersion(root *cobra.Command) {
func NewVersion(l *slog.Logger) *cobra.Command {
cmd := &cobra.Command{
Use: "version",
Short: "Print version",
@ -17,5 +18,5 @@ func NewVersion(root *cobra.Command) {
},
}
root.AddCommand(cmd)
return cmd
}

View File

@ -15,25 +15,21 @@ import (
"github.com/spf13/viper"
)
var conf = koanf.Conf{
Delim: "/",
}
var k = koanf.NewWithConf(conf)
func ReadConfig(l *slog.Logger, c *viper.Viper, cmd *cobra.Command) (*config.Config, error) {
k := koanf.NewWithConf(koanf.Conf{
Delim: "/",
})
func ReadConfig(l *slog.Logger, cmd *cobra.Command) (*config.Config, error) {
filenames := viper.GetStringSlice("config")
for _, filename := range filenames {
for _, filename := range c.GetStringSlice("config") {
var p koanf.Provider
switch {
case filename == "-":
if filename == "-" {
pterm.Debug.Println("reading config from stdin")
if b, err := io.ReadAll(cmd.InOrStdin()); err != nil {
return nil, err
} else {
p = rawbytes.Provider(b)
}
default:
} else {
pterm.Debug.Println("reading config from filename: " + filename)
p = file.Provider(filename)
}

View File

@ -2,12 +2,36 @@ package cmd
import (
"log/slog"
"os"
ptermx "github.com/foomo/sesamy-cli/pkg/pterm"
"github.com/pterm/pterm"
)
func Logger() *slog.Logger {
// Create a new slog handler with the default PTerm logger
handler := NewSlogHandler()
func init() {
pterm.Info.Prefix.Text = "⎈"
pterm.Info.Scope.Style = &pterm.ThemeDefault.DebugMessageStyle
pterm.Debug.Prefix.Text = "⛏︎"
pterm.Debug.Scope.Style = &pterm.ThemeDefault.DebugMessageStyle
pterm.Fatal.Prefix.Text = "⛔︎"
pterm.Fatal.Scope.Style = &pterm.ThemeDefault.DebugMessageStyle
pterm.Error.Prefix.Text = "⛌"
pterm.Error.Scope.Style = &pterm.ThemeDefault.DebugMessageStyle
pterm.Warning.Prefix.Text = "⚠"
pterm.Warning.Scope.Style = &pterm.ThemeDefault.DebugMessageStyle
pterm.Success.Prefix.Text = "✓"
pterm.Success.Scope.Style = &pterm.ThemeDefault.DebugMessageStyle
// Create a new slog logger with the handler
return slog.New(handler)
if scope := os.Getenv("SESAMY_SCOPE"); scope != "" {
pterm.Info.Scope.Text = scope
pterm.Debug.Scope.Text = scope
pterm.Fatal.Scope.Text = scope
pterm.Error.Scope.Text = scope
pterm.Warning.Scope.Text = scope
pterm.Success.Scope.Text = scope
}
}
func NewLogger() *slog.Logger {
return slog.New(ptermx.NewSlogHandler())
}

View File

@ -1,4 +1,4 @@
package cmd
package pterm
import (
"context"
@ -12,6 +12,11 @@ type SlogHandler struct {
attrs []slog.Attr
}
// NewSlogHandler returns a new logging handler that can be intrgrated with log/slog.
func NewSlogHandler() *SlogHandler {
return &SlogHandler{}
}
// Enabled returns true if the given level is enabled.
func (s *SlogHandler) Enabled(ctx context.Context, level slog.Level) bool {
switch level {
@ -78,8 +83,3 @@ func (s *SlogHandler) WithGroup(name string) slog.Handler {
// Grouping is not yet supported by pterm.
return s
}
// NewSlogHandler returns a new logging handler that can be intrgrated with log/slog.
func NewSlogHandler() *SlogHandler {
return &SlogHandler{}
}

View File

@ -124,7 +124,7 @@ func (p *Package) renderTypeBasic(typeName string, typeObject types.Object) {
typeValues := &strings.Builder{}
for _, name := range names {
typeValues.WriteString(fmt.Sprintf("\t%s = %s,\n", name, res[name]))
_, _ = fmt.Fprintf(typeValues, "\t%s = %s,\n", name, res[name])
}
p.code.Body().Tprintn(`
@ -217,9 +217,7 @@ func (p *Package) renderStruct(obj types.Object, typeName string, t *types.Struc
name += "?"
}
typeFields.WriteString(
fmt.Sprintf("\t%s: %s;\n", name, p.tsType(t.Field(i).Type())),
)
_, _ = fmt.Fprintf(typeFields, "\t%s: %s;\n", name, p.tsType(t.Field(i).Type()))
}
if typeFields.Len() == 0 {

View File

@ -1,4 +1,4 @@
package cmd
package utils
import (
"slices"