Browse Source

Writing part of code

main
parent
commit
9e9de1a1d0
  1. 2
      .gitignore
  2. 1
      go.mod
  3. 3
      go.sum
  4. 105
      main.go

2
.gitignore vendored

@ -0,0 +1,2 @@ @@ -0,0 +1,2 @@
*.yml
main

1
go.mod

@ -5,4 +5,5 @@ go 1.17 @@ -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
)

3
go.sum

@ -2,3 +2,6 @@ github.com/jessevdk/go-flags v1.5.0 h1:1jKYvbxEjfUl0fmqTCOfonvskHHXMjBySTLW4y9LF @@ -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=

105
main.go

@ -1,37 +1,102 @@ @@ -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()
}

Loading…
Cancel
Save