aboutsummaryrefslogtreecommitdiff
path: root/cmd/cmd.go
blob: cbfb65b397d7eb1ce2fbfc4674270696353d8f41 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package cmd

import (
	"fmt"
	"os"

	"git.rodere.systems/stream-service/server"
	"github.com/spf13/cobra"
	"github.com/spf13/viper"
)

var (
	// Used for flags.
	cfgFile     string
	userLicense string

	versionCmd = &cobra.Command{
		Use:   "version",
		Short: "Print the version number of stream",
		Long:  `All software has versions. This is stream's`,
		Run: func(cmd *cobra.Command, args []string) {
			fmt.Println("Stream Dummy gRPC/REST API v0.1 -- HEAD")
		},
	}

	serverCmd = &cobra.Command{
		Use:   "serve",
		Short: "Run stream server",
		Long:  `Run the stream server.`,
		Run: func(cmd *cobra.Command, args []string) {
			server.RunServers()
		},
	}

	rootCmd = &cobra.Command{
		Use:   "stream",
		Short: "A dummy service that provides a gRPC/REST API providing Stream Data",
		Long:  "Stream is a dummy service that serves a streaming data from a sink via gRPC/REST API.",
	}
)

// Execute executes the root command.
func Execute() error {
	return rootCmd.Execute()
}

func init() {
	cobra.OnInitialize(initConfig)

	rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $STREAM_PATH/.stream.toml)")
	rootCmd.PersistentFlags().StringP("author", "a", "Carlos Sosa", "author name for copyright attribution")
	rootCmd.PersistentFlags().StringVarP(&userLicense, "license", "l", "AGPL-3", "name of license for the project")
	rootCmd.PersistentFlags().Bool("viper", true, "use Viper for configuration")
	viper.BindPFlag("author", rootCmd.PersistentFlags().Lookup("author"))
	viper.BindPFlag("useViper", rootCmd.PersistentFlags().Lookup("viper"))
	viper.SetDefault("author", "Carlos Sosa <gnusosa@gnusosa.net>")
	viper.SetDefault("license", "AGPL-3")

	//rootCmd.AddCommand(addCmd)
	rootCmd.AddCommand(versionCmd)
	rootCmd.AddCommand(serverCmd)
}

func er(msg interface{}) {
	fmt.Println("Error:", msg)
	os.Exit(1)
}

func initConfig() {
	if cfgFile != "" {
		// Use config file from the flag.
		viper.SetConfigFile(cfgFile)
	} else {
		// Search config in home directory with name ".cobra" (without extension).
		viper.AddConfigPath("./")
		viper.SetConfigName(".stream.toml")
	}

	viper.AutomaticEnv()

	if err := viper.ReadInConfig(); err == nil {
		fmt.Println("Using config file:", viper.ConfigFileUsed())
	} else {
		fmt.Println("Error loading config file:", viper.ConfigFileUsed())
		fmt.Println("Error message from loading config file:", err)
	}

}