package main

import (
	"flag"
	"log"

	"github.com/mendersoftware/artifacts/config"
	"github.com/spf13/viper"
)

func main() {

	var configPath string
	flag.StringVar(&configPath, "config", "config.yaml", "Configuration file path. Supports JSON, TOML, YAML and HCL formatted configs.")

	flag.Parse()

	c := viper.New()
	c.SetConfigFile(configPath)

	// Set default values for config
	SetDefaultConfigs(c)

	// Find and read the config file
	if err := c.ReadInConfig(); err != nil {
		log.Fatalln(err)
	}

	// Validate config
	if err := config.ValidateConfig(c,
		ValidateAwsAuth,
		ValidateHttps,
	); err != nil {
		log.Fatalln(err)
	}

	log.Fatalln(RunServer(c))
}

func SetDefaultConfigs(config *viper.Viper) {
	config.SetDefault(SettingListen, SettingListenDefault)
	config.SetDefault(SettingAwsS3Region, SettingAwsS3RegionDefault)
	config.SetDefault(SettingAweS3Bucket, SettingAwsS3BucketDefault)
}

package main

import (
	"flag"
	"fmt"
	"log"
	"os"

	"github.com/mendersoftware/artifacts/config"
	"github.com/spf13/viper"
)

func main() {

	var configPath string
	var printVersion bool
	flag.StringVar(&configPath, "config", "config.yaml", "Configuration file path. Supports JSON, TOML, YAML and HCL formatted configs.")
	flag.BoolVar(&printVersion, "version", false, "Show version")

	flag.Parse()

	if printVersion {
		fmt.Println(CreateVersionString())
		os.Exit(0)
	}

	configuration, err := HandleConfigFile(configPath)
	if err != nil {
		log.Fatalln(err)
	}

	log.Fatalln(RunServer(configuration))
}

func HandleConfigFile(filePath string) (config.ConfigReader, error) {

	c := viper.New()
	c.SetConfigFile(filePath)

	// Set default values for config
	SetDefaultConfigs(c)

	// Find and read the config file
	if err := c.ReadInConfig(); err != nil {
		return nil, err
	}

	// Validate config
	if err := config.ValidateConfig(c,
		ValidateAwsAuth,
		ValidateHttps,
	); err != nil {
		return nil, err
	}

	return c, nil
}

func SetDefaultConfigs(config *viper.Viper) {
	config.SetDefault(SettingListen, SettingListenDefault)
	config.SetDefault(SettingAwsS3Region, SettingAwsS3RegionDefault)
	config.SetDefault(SettingAweS3Bucket, SettingAwsS3BucketDefault)
}

Add -version command line flag to print application version.
Add -version command line flag to print application version.
