Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sesamy cli v0.3.x #33

Merged
merged 6 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ linters-settings:
- name: unhandled-error
arguments:
- "fmt.Println"
- "viper.BindPFlag"
- "strings.Builder.WriteString"
# TODO remove
- name: deep-exit
Expand Down
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@

## === Tasks ===

.PHONY: schema
## Open go schemas
schema:
@helm schema-gen sesamy.yaml > sesamy.schema.json

.PHONY: doc
## Open go docs
doc:
Expand Down
34 changes: 34 additions & 0 deletions cmd/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package cmd

import (
"encoding/json"
"fmt"

pkgcmd "github.com/foomo/sesamy-cli/pkg/cmd"
"github.com/spf13/cobra"
)

func NewConfig(root *cobra.Command) {
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)
if err != nil {
return err
}

out, err := json.MarshalIndent(cfg, "", " ")
if err != nil {
return err
}

fmt.Println(string(out))
return nil
},
}

root.AddCommand(cmd)
}
113 changes: 23 additions & 90 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,109 +1,42 @@
package cmd

import (
"bytes"
"io"
"log/slog"
"os"

"github.com/foomo/sesamy-cli/pkg/config"
"github.com/mitchellh/mapstructure"
"github.com/pterm/pterm"
pkgcmd "github.com/foomo/sesamy-cli/pkg/cmd"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var (
logger *slog.Logger
verbose bool
cfgFilename string
cfg *config.Config
)

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "sesamy",
Short: "Server Side Tag Management System",
}

// 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)
}
}
var root *cobra.Command

func init() {
cobra.OnInitialize(initConfig)

// 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().BoolVarP(&verbose, "verbose", "v", false, "output debug information")
rootCmd.PersistentFlags().StringVarP(&cfgFilename, "config", "c", "", "config file (default is sesamy.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")
root = NewRoot()
NewConfig(root)
NewVersion(root)
NewTagmanager(root)
NewTypeScript(root)
cobra.OnInitialize(pkgcmd.InitConfig)
}

// initConfig reads in config file and ENV variables if set.
func initConfig() {
viper.SetConfigType("yaml")
if cfgFilename == "-" {
// do nothing
} else if cfgFilename != "" {
// Use config file from the flag.
viper.SetConfigFile(cfgFilename)
} else {
// Search config in home directory with name ".sesamy" (without extension).
viper.AddConfigPath(".")
viper.SetConfigName("sesamy")
}

// read in environment variables that match
// viper.EnvKeyReplacer(strings.NewReplacer(".", "_"))
// viper.SetEnvPrefix("SESAMY")
// viper.AutomaticEnv()

plogger := pterm.DefaultLogger.WithTime(false)
if verbose {
plogger = plogger.WithLevel(pterm.LogLevelTrace).WithCaller(true)
// NewRoot represents the base command when called without any subcommands
func NewRoot() *cobra.Command {
cmd := &cobra.Command{
Use: "sesamy",
Short: "Server Side Tag Management System",
}
cmd.PersistentFlags().BoolP("verbose", "v", false, "output debug information")
_ = viper.BindPFlag("verbose", cmd.PersistentFlags().Lookup("verbose"))

// Create a new slog handler with the default PTerm logger
handler := pterm.NewSlogHandler(plogger)

// Create a new slog logger with the handler
logger = slog.New(handler)
cmd.PersistentFlags().StringP("config", "c", "sesamy.yaml", "config file (default is sesamy.yaml)")
_ = viper.BindPFlag("config", cmd.PersistentFlags().Lookup("config"))
return cmd
}

func preRunReadConfig(cmd *cobra.Command, args []string) error {
if cfgFilename == "-" {
logger.Debug("using config from stdin")
b, err := io.ReadAll(cmd.InOrStdin())
if err != nil {
return err
}
if err := viper.ReadConfig(bytes.NewBuffer(b)); err != nil {
return err
}
} else {
logger.Debug("using config file", "filename", viper.ConfigFileUsed())
if err := viper.ReadInConfig(); err != nil {
return err
}
}
// logger.Debug("config", logger.ArgsFromMap(viper.AllSettings()))

if err := viper.Unmarshal(&cfg, func(decoderConfig *mapstructure.DecoderConfig) {
decoderConfig.TagName = "yaml"
}); err != nil {
return err
// 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() {
if err := root.Execute(); err != nil {
os.Exit(1)
}

return nil
}
13 changes: 5 additions & 8 deletions cmd/tagmanager.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
package cmd

import (
"github.com/foomo/sesamy-cli/cmd/tagmanager"
"github.com/spf13/cobra"
)

// NewTagmanagerCmd represents the tagmanager command
func NewTagmanagerCmd(root *cobra.Command) *cobra.Command {
// NewTagmanager represents the tagmanager command
func NewTagmanager(root *cobra.Command) *cobra.Command {
cmd := &cobra.Command{
Use: "tagmanager",
Short: "Provision Google Tag Manager containers",
}

tagmanager.NewServer(cmd)
tagmanager.NewWeb(cmd)
root.AddCommand(cmd)

return cmd
}

func init() {
cmd := NewTagmanagerCmd(rootCmd)
NewTagManagerServerCmd(cmd)
NewTagmanagerWebCmd(cmd)
}
103 changes: 103 additions & 0 deletions cmd/tagmanager/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package tagmanager

import (
pkgcmd "github.com/foomo/sesamy-cli/pkg/cmd"
conversionlinkerprovider "github.com/foomo/sesamy-cli/pkg/provider/conversionlinker"
facebookprovider "github.com/foomo/sesamy-cli/pkg/provider/facebook"
googleadsprovider "github.com/foomo/sesamy-cli/pkg/provider/googleads"
googleanalyticsprovider "github.com/foomo/sesamy-cli/pkg/provider/googleanalytics"
googletagprovider "github.com/foomo/sesamy-cli/pkg/provider/googletag"
googletagmanagerprovider "github.com/foomo/sesamy-cli/pkg/provider/googletagmanager"
umamiprovider "github.com/foomo/sesamy-cli/pkg/provider/umami"
"github.com/foomo/sesamy-cli/pkg/tagmanager"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// NewServer represents the server command
func NewServer(root *cobra.Command) {
cmd := &cobra.Command{
Use: "server",
Short: "Provision Google Tag Manager Server Container",
RunE: func(cmd *cobra.Command, args []string) error {
l := pkgcmd.Logger()

tags, err := cmd.Flags().GetStringSlice("tags")
if err != nil {
return errors.Wrap(err, "error reading tags flag")
}

cfg, err := pkgcmd.ReadConfig(l, cmd)
if err != nil {
return err
}

tm, err := tagmanager.New(
cmd.Context(),
l,
cfg.GoogleTagManager.AccountID,
cfg.GoogleTagManager.ServerContainer,
tagmanager.WithRequestQuota(cfg.GoogleAPI.RequestQuota),
tagmanager.WithClientOptions(cfg.GoogleAPI.GetClientOption()),
)
if err != nil {
return err
}

if pkgcmd.Tag(googletagprovider.Tag, tags) {
if err := googletagprovider.Server(tm); err != nil {
return errors.Wrap(err, "failed to provision google tag")
}
}

if pkgcmd.Tag(googletagmanagerprovider.Tag, tags) {
if err := googletagmanagerprovider.Server(tm, cfg.GoogleTagManager); err != nil {
return errors.Wrap(err, "failed to provision google tag manager")
}
}

if cfg.GoogleAnalytics.Enabled && pkgcmd.Tag(googleanalyticsprovider.Tag, tags) {
l.Info("🅿️ Running provider", "name", googleanalyticsprovider.Name)
if err := googleanalyticsprovider.Server(tm, cfg.GoogleAnalytics, cfg.RedactVisitorIP); err != nil {
return errors.Wrap(err, "failed to provision google analytics")
}
}

if cfg.ConversionLinker.Enabled && pkgcmd.Tag(conversionlinkerprovider.Tag, tags) {
l.Info("🅿️ Running provider", "name", conversionlinkerprovider.Name)
if err := conversionlinkerprovider.Server(tm, cfg.ConversionLinker); err != nil {
return errors.Wrap(err, "failed to provision conversion linker")
}
}

if cfg.Umami.Enabled && pkgcmd.Tag(umamiprovider.Tag, tags) {
l.Info("🅿️ Running provider", "name", umamiprovider.Name)
if err := umamiprovider.Server(tm, cfg.Umami); err != nil {
return errors.Wrap(err, "failed to provision umammi")
}
}

if cfg.Facebook.Enabled && pkgcmd.Tag(facebookprovider.Tag, tags) {
l.Info("🅿️ Running provider", "name", facebookprovider.Name)
if err := facebookprovider.Server(l, tm, cfg.Facebook); err != nil {
return errors.Wrap(err, "failed to provision facebook")
}
}

if cfg.GoogleAds.Enabled && pkgcmd.Tag(googleadsprovider.Tag, tags) {
l.Info("🅿️ Running provider", "name", googleadsprovider.Name)
if err := googleadsprovider.Server(l, tm, cfg.GoogleAds); err != nil {
return errors.Wrap(err, "failed to provision google ads")
}
}

return nil
},
}

cmd.Flags().StringSlice("tags", nil, "list of tags to provision")
_ = viper.BindPFlag("tags", cmd.Flags().Lookup("tags"))

root.AddCommand(cmd)
}
63 changes: 63 additions & 0 deletions cmd/tagmanager/web.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package tagmanager

import (
pkgcmd "github.com/foomo/sesamy-cli/pkg/cmd"
googleanaylticsprovider "github.com/foomo/sesamy-cli/pkg/provider/googleanalytics"
googletagprovider "github.com/foomo/sesamy-cli/pkg/provider/googletag"
"github.com/foomo/sesamy-cli/pkg/tagmanager"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// NewWeb represents the web command
func NewWeb(root *cobra.Command) {
cmd := &cobra.Command{
Use: "web",
Short: "Provision Google Tag Manager Web Container",
RunE: func(cmd *cobra.Command, args []string) error {
l := pkgcmd.Logger()

tags, err := cmd.Flags().GetStringSlice("tags")
if err != nil {
return errors.Wrap(err, "error reading tags flag")
}

cfg, err := pkgcmd.ReadConfig(l, cmd)
if err != nil {
return err
}

tm, err := tagmanager.New(
cmd.Context(),
l,
cfg.GoogleTagManager.AccountID,
cfg.GoogleTagManager.WebContainer,
tagmanager.WithRequestQuota(cfg.GoogleAPI.RequestQuota),
tagmanager.WithClientOptions(cfg.GoogleAPI.GetClientOption()),
)
if err != nil {
return err
}

if pkgcmd.Tag(googletagprovider.Tag, tags) {
if err := googletagprovider.Web(tm, cfg.GoogleTag); err != nil {
return errors.Wrap(err, "failed to provision google tag")
}
}

if cfg.GoogleAnalytics.Enabled && pkgcmd.Tag(googleanaylticsprovider.Tag, tags) {
if err := googleanaylticsprovider.Web(tm, cfg.GoogleAnalytics); err != nil {
return errors.Wrap(err, "failed to provision google analytics tag")
}
}

return nil
},
}

cmd.Flags().StringSlice("tags", nil, "list of tags to provision")
_ = viper.BindPFlag("tags", cmd.Flags().Lookup("tags"))

root.AddCommand(cmd)
}
Loading