Skip to content

Commit

Permalink
Merge pull request #33 from foomo/sesamy-cli-0.3.x
Browse files Browse the repository at this point in the history
Sesamy cli v0.3.x
  • Loading branch information
franklinkim authored Jul 18, 2024
2 parents 544e2e1 + 6b95b1f commit 42e5eb9
Show file tree
Hide file tree
Showing 94 changed files with 2,693 additions and 1,588 deletions.
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

0 comments on commit 42e5eb9

Please sign in to comment.