4 changed files with 91 additions and 20 deletions
@ -1,37 +1,102 @@ |
|||||||
package main |
package main |
||||||
|
|
||||||
import ( |
import ( |
||||||
"fmt" |
"log" |
||||||
"strings" |
"os" |
||||||
|
|
||||||
"github.com/jessevdk/go-flags" |
"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 { |
type Options struct { |
||||||
Telegram bool `short:"t" long:"telegram-send" description:"Send to telegram"` |
FileParse string `short:"f" long:"fileparse" description:"File for parce (rss xml)"` |
||||||
Telegramtoken string `short:"k" long:"telegram-token" description:"Token for send to telegram"` |
ConfigPath string `short:"c" long:"configpath" description:"Config file path"` |
||||||
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"` |
var ConfigPath = "/etc/ssender/config.yml" |
||||||
FBtoken string `short:"m" long:"fb-token" description:"Token for send to Facebook"` |
|
||||||
|
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() { |
func main() { |
||||||
|
// Parse flags
|
||||||
var options Options |
var options Options |
||||||
var parser = flags.NewParser(&options, flags.Default) |
var parser = flags.NewParser(&options, flags.Default) |
||||||
// parser.CommandHandler = func(command flags.Commander, args []string) error {
|
if _, err := parser.Parse(); err != nil { |
||||||
// print(options.Telegram)
|
switch flagsErr := err.(type) { |
||||||
// }
|
case flags.ErrorType: |
||||||
args, err := parser.Parse() |
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 { |
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) |
// Run send data depended on configuration options
|
||||||
fmt.Printf("Telegram token: %s\n", options.Telegramtoken) |
log.Println("Run send process.") |
||||||
fmt.Printf("VK send: %v\n", options.VK) |
cfg.Run() |
||||||
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, " ")) |
|
||||||
} |
} |
||||||
|
|||||||
Loading…
Reference in new issue