From 9e9de1a1d099b74401037e481740a430bfb1b17b Mon Sep 17 00:00:00 2001 From: "Andrey Ivanov (NetMoose)" Date: Tue, 22 Feb 2022 22:08:10 +0500 Subject: [PATCH] Writing part of code --- .gitignore | 2 + go.mod | 1 + go.sum | 3 ++ main.go | 105 +++++++++++++++++++++++++++++++++++++++++++---------- 4 files changed, 91 insertions(+), 20 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..77c271d --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.yml +main \ No newline at end of file diff --git a/go.mod b/go.mod index 23e2978..2d420a8 100644 --- a/go.mod +++ b/go.mod @@ -5,4 +5,5 @@ go 1.17 require ( github.com/jessevdk/go-flags v1.5.0 // indirect golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4 // indirect + gopkg.in/yaml.v2 v2.4.0 // indirect ) diff --git a/go.sum b/go.sum index df31363..07c6e45 100644 --- a/go.sum +++ b/go.sum @@ -2,3 +2,6 @@ github.com/jessevdk/go-flags v1.5.0 h1:1jKYvbxEjfUl0fmqTCOfonvskHHXMjBySTLW4y9LF github.com/jessevdk/go-flags v1.5.0/go.mod h1:Fw0T6WPc1dYxT4mKEZRfG5kJhaTDP9pj1c2EWnYs/m4= golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4 h1:EZ2mChiOa8udjfp6rRmswTbtZN/QzUQp4ptM4rnjHvc= golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= diff --git a/main.go b/main.go index d95d785..727ec24 100644 --- a/main.go +++ b/main.go @@ -1,37 +1,102 @@ package main import ( - "fmt" - "strings" + "log" + "os" "github.com/jessevdk/go-flags" + "gopkg.in/yaml.v2" ) +type Config struct { + Telegram struct { + Send bool `yaml:"send"` + Token string `yaml:"token"` + } `yaml:"telegram"` + VK struct { + Send bool `yaml:"send"` + Token string `yaml:"token"` + } `yaml:"vk"` + Facebook struct { + Send bool `yaml:"send"` + Token string `yaml:"token"` + } `yaml:"facebook"` +} + +func NewConfig(configPath string) (*Config, error) { + // Create config structure + config := &Config{} + + // Open config file + file, err := os.Open(configPath) + if err != nil { + return nil, err + } + defer file.Close() + + // Init new YAML decode + d := yaml.NewDecoder(file) + + // Start YAML decoding from file + if err := d.Decode(&config); err != nil { + return nil, err + } + + return config, nil +} + type Options struct { - Telegram bool `short:"t" long:"telegram-send" description:"Send to telegram"` - Telegramtoken string `short:"k" long:"telegram-token" description:"Token for send to telegram"` - VK bool `short:"v" long:"vk-send" description:"Send to vk"` - VKtoken string `short:"b" long:"vk-token" description:"Token for send to VK"` - Facebook bool `short:"f" long:"fb-send" description:"Send to Facebook"` - FBtoken string `short:"m" long:"fb-token" description:"Token for send to Facebook"` + FileParse string `short:"f" long:"fileparse" description:"File for parce (rss xml)"` + ConfigPath string `short:"c" long:"configpath" description:"Config file path"` +} + +var ConfigPath = "/etc/ssender/config.yml" + +func (config Config) Run() { + if config.Telegram.Send { + log.Println("Send to telegram.") + } + if config.VK.Send { + log.Println("Send to VK.") + } + if config.Facebook.Send { + log.Println("Send to Facebook.") + } } func main() { + // Parse flags var options Options var parser = flags.NewParser(&options, flags.Default) - // parser.CommandHandler = func(command flags.Commander, args []string) error { - // print(options.Telegram) - // } - args, err := parser.Parse() + if _, err := parser.Parse(); err != nil { + switch flagsErr := err.(type) { + case flags.ErrorType: + if flagsErr == flags.ErrHelp { + os.Exit(0) + } + os.Exit(1) + default: + os.Exit(1) + } + } + log.Println("Flags processed.") + + if options.ConfigPath != "" { + log.Printf("Config from: %s\n", options.ConfigPath) + ConfigPath = options.ConfigPath + } + + // Get config + cfg, err := NewConfig(ConfigPath) if err != nil { - panic(err) + log.Fatal(err) } + log.Println("Config processed.") + + // Parse file + log.Printf("Parse file ... \n") - fmt.Printf("Telegram send: %v\n", options.Telegram) - fmt.Printf("Telegram token: %s\n", options.Telegramtoken) - fmt.Printf("VK send: %v\n", options.VK) - fmt.Printf("VK token: %s\n", options.VKtoken) - fmt.Printf("Facebook send: %v\n", options.Facebook) - fmt.Printf("Facebook token: %s\n", options.FBtoken) - fmt.Printf("Remaining args: %s\n", strings.Join(args, " ")) + // Run send data depended on configuration options + log.Println("Run send process.") + cfg.Run() }