diff options
author | pg9182 <96569817+pg9182@users.noreply.github.com> | 2022-10-21 03:57:04 -0400 |
---|---|---|
committer | pg9182 <96569817+pg9182@users.noreply.github.com> | 2022-10-21 05:29:13 -0400 |
commit | eca0a756c7eccb83e7efb6f6c8a1b4ca1e477a79 (patch) | |
tree | 60e0c7a48a78b5d707c591aff5c4918838f20184 /cmd/atlas | |
parent | c00205a5899265e7d4f9d92590651606e10baee4 (diff) | |
download | Atlas-eca0a756c7eccb83e7efb6f6c8a1b4ca1e477a79.tar.gz Atlas-eca0a756c7eccb83e7efb6f6c8a1b4ca1e477a79.zip |
cmd/atlas: Implement server command
Diffstat (limited to 'cmd/atlas')
-rw-r--r-- | cmd/atlas/main.go | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/cmd/atlas/main.go b/cmd/atlas/main.go new file mode 100644 index 0000000..a9251b5 --- /dev/null +++ b/cmd/atlas/main.go @@ -0,0 +1,100 @@ +// Command atlas TODO. +package main + +import ( + "context" + "errors" + "fmt" + "os" + "os/signal" + "syscall" + + "github.com/hashicorp/go-envparse" + _ "github.com/mattn/go-sqlite3" + "github.com/pg9182/atlas/pkg/atlas" + "github.com/spf13/pflag" +) + +var opt struct { + Help bool +} + +func init() { + pflag.BoolVarP(&opt.Help, "help", "h", false, "Show this help text") +} + +func main() { + pflag.Parse() + + if pflag.NArg() > 1 || opt.Help { + fmt.Printf("usage: %s [options] [env_file]\n\noptions:\n%s\nnote: if env_file is provided, config from the environment is ignored\n", os.Args[0], pflag.CommandLine.FlagUsages()) + if opt.Help { + os.Exit(2) + } + os.Exit(0) + } + + var e []string + if pflag.NArg() == 0 { + e = os.Environ() + } else { + if x, err := readEnv(pflag.Arg(0)); err == nil { + e = x + } else { + fmt.Fprintf(os.Stderr, "error: read env file: %v\n", err) + os.Exit(1) + } + if v, ok := os.LookupEnv("NOTIFY_SOCKET"); ok { + e = append(e, "NOTIFY_SOCKET="+v) + } + } + + var c atlas.Config + if err := c.UnmarshalEnv(e, false); err != nil { + fmt.Fprintf(os.Stderr, "error: parse config: %v\n", err) + os.Exit(1) + } + + s, err := atlas.NewServer(&c) + if err != nil { + fmt.Fprintf(os.Stderr, "error: initialize server: %v\n", err) + os.Exit(1) + } + + ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) + defer stop() + + hch := make(chan os.Signal, 1) + signal.Notify(hch, syscall.SIGHUP) + + go func() { + for range hch { + fmt.Println("got SIGHUP") + s.HandleSIGHUP() + } + }() + + if err := s.Run(ctx); err != nil && !errors.Is(err, context.Canceled) { + fmt.Fprintf(os.Stderr, "error: run server: %v\n", err) + os.Exit(1) + } +} + +func readEnv(name string) ([]string, error) { + f, err := os.Open(name) + if err != nil { + return nil, err + } + defer f.Close() + + m, err := envparse.Parse(f) + if err != nil { + return nil, err + } + + var r []string + for k, v := range m { + r = append(r, k+"="+v) + } + return r, nil +} |