Commit 34d49feb authored by ale's avatar ale
Browse files

Added skeleton of main program

parent fb207eba
package main
import (
"context"
"flag"
"log"
"os"
"os/signal"
"syscall"
"github.com/google/subcommands"
)
type daemonCommand struct {
configPath string
httpAddr string
}
func (c *daemonCommand) Name() string { return "daemon" }
func (c *daemonCommand) Synopsis() string { return "start the backup daemon" }
func (c *daemonCommand) Usage() string {
return `daemon [<flags>]:
Start the backup daemon.
`
}
func (c *daemonCommand) SetFlags(f *flag.FlagSet) {
f.StringVar(&c.configPath, "config", "/etc/tabacco/config.yml", "configuration `file`")
f.StringVar(&c.httpAddr, "http-addr", ":5331", "listen `address` for the HTTP server exporting metrics and debugging")
}
func (c *daemonCommand) Execute(ctx context.Context, f *flag.FlagSet, args ...interface{}) subcommands.ExitStatus {
if f.NArg() > 0 {
log.Printf("error: too many arguments")
return subcommands.ExitUsageError
}
return subcommands.ExitSuccess
}
// Wrapper that installs a SIGTERM handler that will cancel the
// Context passed to the Execute() method.
type withSignalHandlers struct {
subcommands.Command
}
func (c *withSignalHandlers) Execute(ctx context.Context, f *flag.FlagSet, args ...interface{}) subcommands.ExitStatus {
// Set up a global root context that handles asynchronous
// termination via signal.
ctx, cancel := context.WithCancel(ctx)
defer cancel()
termCh := make(chan os.Signal, 1)
go func() {
sig := <-termCh
log.Printf("received signal %s, exiting", sig.String())
cancel()
}()
signal.Notify(termCh, syscall.SIGINT, syscall.SIGTERM)
return c.Command.Execute(ctx, f, args...)
}
func init() {
subcommands.Register(&withSignalHandlers{&daemonCommand{}}, "")
subcommands.Register(subcommands.HelpCommand(), "help")
subcommands.Register(subcommands.CommandsCommand(), "help")
subcommands.Register(subcommands.FlagsCommand(), "help")
}
func main() {
flag.Parse()
log.SetFlags(0)
os.Exit(int(subcommands.Execute(context.Background())))
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment