Skip to content

Commit

Permalink
finish initial start command implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
severindellsperger committed Jan 10, 2024
1 parent 664f2a3 commit 8c3cb70
Show file tree
Hide file tree
Showing 11 changed files with 253 additions and 69 deletions.
Binary file removed __debug_bin468058418
Binary file not shown.
13 changes: 8 additions & 5 deletions cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,20 @@ var startCmd = &cobra.Command{
if err != nil {
log.Fatalf("Error creating config: %v\n", err)
}
if err := defaultConfig.WatchConfigChange(); err != nil {
log.Fatalf("Error watching config change: %v\n", err)
}
unprocessedMsgChan := make(chan consumer.Message)
processedMsgChan := make(chan consumer.Message)
consumer := consumer.NewKafkaConsumer(KafkaBroker, ReceiverTopic, unprocessedMsgChan)
if err := consumer.Init(); err != nil {
log.Fatalf("Error initializing receiver: %v\n", err)
}

publisher := publisher.NewDefaultPublisher(KafkaBroker, PublisherTopic, processedMsgChan)
if err := publisher.Init(); err != nil {
log.Fatalf("Error initializing publisher: %v\n", err)
}
processor := processor.NewDefaultProcessor(defaultConfig, unprocessedMsgChan, processedMsgChan, helpers.NewDefaultHelper())
publisher := publisher.NewDefaultPublisher(processedMsgChan)

defaultService := service.NewDefaultService(defaultConfig, consumer, processor, publisher)
if err := defaultService.Start(); err != nil {
Expand All @@ -46,9 +51,7 @@ var startCmd = &cobra.Command{

<-signalChan
log.Info("Received interrupt signal, shutting down")
if err := defaultService.Stop(); err != nil {
log.Fatalf("Error stopping service: %v\n", err)
}
defaultService.Stop()
},
}

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ require (
github.com/IBM/sarama v1.42.1
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/influxdata/line-protocol/v2 v2.2.1
github.com/knadh/koanf v1.5.0
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/sirupsen/logrus v1.9.3
Expand Down
14 changes: 14 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU=
github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M=
github.com/frankban/quicktest v1.11.0/go.mod h1:K+q6oSqb0W0Ininfk863uOk1lMy69l/P6txr3mVT54s=
github.com/frankban/quicktest v1.11.2/go.mod h1:K+q6oSqb0W0Ininfk863uOk1lMy69l/P6txr3mVT54s=
github.com/frankban/quicktest v1.13.0/go.mod h1:qLE0fzW0VuyUAJgPU19zByoIr0HtCHN/r/VLSOOIySU=
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
Expand Down Expand Up @@ -95,6 +98,7 @@ github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
Expand Down Expand Up @@ -149,6 +153,12 @@ github.com/hjson/hjson-go/v4 v4.0.0 h1:wlm6IYYqHjOdXH1gHev4VoXCaW20HdQAGCxdOEEg2
github.com/hjson/hjson-go/v4 v4.0.0/go.mod h1:KaYt3bTw3zhBjYqnXkYywcYctk0A2nxeEFTse3rH13E=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/influxdata/line-protocol-corpus v0.0.0-20210519164801-ca6fa5da0184/go.mod h1:03nmhxzZ7Xk2pdG+lmMd7mHDfeVOYFyhOgwO61qWU98=
github.com/influxdata/line-protocol-corpus v0.0.0-20210922080147-aa28ccfb8937/go.mod h1:BKR9c0uHSmRgM/se9JhFHtTT7JTO67X23MtKMHtZcpo=
github.com/influxdata/line-protocol/v2 v2.0.0-20210312151457-c52fdecb625a/go.mod h1:6+9Xt5Sq1rWx+glMgxhcg2c0DUaehK+5TDcPZ76GypY=
github.com/influxdata/line-protocol/v2 v2.1.0/go.mod h1:QKw43hdUBg3GTk2iC3iyCxksNj7PX9aUSeYOYE/ceHY=
github.com/influxdata/line-protocol/v2 v2.2.1 h1:EAPkqJ9Km4uAxtMRgUubJyqAr6zgWM0dznKMLRauQRE=
github.com/influxdata/line-protocol/v2 v2.2.1/go.mod h1:DmB3Cnh+3oxmG6LOBIxce4oaL4CPj3OmMPgvauXh+tM=
github.com/jcmturner/aescts/v2 v2.0.0 h1:9YKLH6ey7H4eDBXW8khjYslgyqG2xZikXP0EQFKrle8=
github.com/jcmturner/aescts/v2 v2.0.0/go.mod h1:AiaICIRyfYg35RUkr8yESTqvSy7csK90qZ5xfvvsoNs=
github.com/jcmturner/dnsutils/v2 v2.0.0 h1:lltnkeZGL0wILNvrNiVCR6Ro5PGU/SeBvVO/8c/iPbo=
Expand Down Expand Up @@ -181,6 +191,7 @@ github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxv
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
Expand Down Expand Up @@ -220,6 +231,7 @@ github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lN
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/npillmayer/nestext v0.1.3/go.mod h1:h2lrijH8jpicr25dFY+oAJLyzlya6jhnuG+zWp9L0Uk=
github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA=
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
Expand Down Expand Up @@ -451,6 +463,7 @@ gopkg.in/asn1-ber.v1 v1.0.0-20181015200546-f715ec2f112d/go.mod h1:cuepJuh7vyXfUy
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/square/go-jose.v2 v2.3.1/go.mod h1:M9dMgbHiYLoDGQrXy7OpJDJWiKiU//h+vD76mk0e1AI=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Expand All @@ -461,6 +474,7 @@ 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-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
39 changes: 30 additions & 9 deletions pkg/config/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ type DefaultConfig struct {
userHome string
fileName string
configPath string
fullFileLocation string
fullfileLocation string
clabName string
clabNameKey string
fileProvider *file.File
helper helpers.Helper
}

Expand All @@ -47,11 +48,15 @@ func (config *DefaultConfig) setConfigFileName(configName string) {
}
}

func (config *DefaultConfig) GetConfigPath() string {
return config.fullfileLocation

Check warning on line 52 in pkg/config/default.go

View check run for this annotation

Codecov / codecov/patch

pkg/config/default.go#L51-L52

Added lines #L51 - L52 were not covered by tests
}

func (config *DefaultConfig) setConfigPath() {
config.configPath = config.userHome + "/.clab-telemetry-linker"
}
func (config *DefaultConfig) setConfigFileLocation() {
config.fullFileLocation = config.configPath + "/" + config.fileName
config.fullfileLocation = config.configPath + "/" + config.fileName
}

func (config *DefaultConfig) setClabName(clabName string) error {
Expand All @@ -73,8 +78,8 @@ func (config *DefaultConfig) setClabName(clabName string) error {
}

func (config *DefaultConfig) doesConfigExist() (error, bool) {
config.log.Debugln("Check if config file exists:", config.fullFileLocation)
if _, err := os.Stat(config.fullFileLocation); err != nil {
config.log.Debugln("Check if config file exists:", config.fullfileLocation)
if _, err := os.Stat(config.fullfileLocation); err != nil {
if os.IsNotExist(err) {
return nil, false
}
Expand All @@ -88,7 +93,7 @@ func (config *DefaultConfig) createConfig() error {
if err := os.MkdirAll(config.configPath, 0755); err != nil {
return err
}
configFile, err := os.Create(config.fullFileLocation)
configFile, err := os.Create(config.fullfileLocation)
if err != nil {
return err
}
Expand All @@ -97,8 +102,9 @@ func (config *DefaultConfig) createConfig() error {
}

func (config *DefaultConfig) readConfig() error {
config.log.Debugln("Read config file: ", config.fullFileLocation)
if err := config.koanfInstance.Load(file.Provider(config.fullFileLocation), yaml.Parser()); err != nil {
config.log.Infoln("Read config file: ", config.fullfileLocation)
config.fileProvider = file.Provider(config.fullfileLocation)
if err := config.koanfInstance.Load(config.fileProvider, yaml.Parser()); err != nil {
return err
}
return nil
Expand All @@ -119,6 +125,21 @@ func (config *DefaultConfig) InitConfig() error {
}
return nil
}
func (config *DefaultConfig) WatchConfigChange() error {
if err := config.fileProvider.Watch(func(event interface{}, err error) {

if err != nil {
config.log.Errorf("Error watching config file: %v", err)
}
config.log.Debugln("Config file changed")
if err := config.readConfig(); err != nil {
config.log.Errorf("Error reading config file: %v", err)
}
}); err != nil {
return err
}
return nil

Check warning on line 141 in pkg/config/default.go

View check run for this annotation

Codecov / codecov/patch

pkg/config/default.go#L128-L141

Added lines #L128 - L141 were not covered by tests
}

func (config *DefaultConfig) DeleteValue(key string) {
config.log.Debugln("Delete value from config: ", key)
Expand All @@ -144,13 +165,13 @@ func (config *DefaultConfig) GetValue(key string) string {
}

func (config *DefaultConfig) WriteConfig() error {
config.log.Debugln("Write config file: ", config.fullFileLocation)
config.log.Debugln("Write config file: ", config.fullfileLocation)
data, err := config.koanfInstance.Marshal(yaml.Parser())
if err != nil {
config.log.Errorf("error marshalling config: %v", err)
return err
}
if err := os.WriteFile(config.fullFileLocation, data, 0644); err != nil {
if err := os.WriteFile(config.fullfileLocation, data, 0644); err != nil {
config.log.Errorf("error writing config: %v", err)
return err
}
Expand Down
Loading

0 comments on commit 8c3cb70

Please sign in to comment.