root.go 4.3 KB
Newer Older
Heiko Reese's avatar
 
Heiko Reese committed
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
// Copyright © 2018 Heiko Reese
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice,
//    this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
//    this list of conditions and the following disclaimer in the documentation
//    and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors
//    may be used to endorse or promote products derived from this software
//    without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.

package cmd

import (
	"fmt"
	"os"

	"path/filepath"

Heiko Reese's avatar
   
Heiko Reese committed
38
39
40
	"log"

	"github.com/mitchellh/go-homedir"
Heiko Reese's avatar
 
Heiko Reese committed
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
	"github.com/spf13/cobra"
	"github.com/spf13/viper"
)

const configsubdir = ".config"

var cfgFile string

// rootCmd represents the base command when called without any subcommands
var (
	rootCmd = &cobra.Command{
		Use:   "dfnpkitool",
		Short: "cli interface to the dfn-pki soap api.",
		// Long: ``,
		// Uncomment the following line if your bare application
		// has an action associated with it:
57
58
		//Run: func(cmd *cobra.Command, args []string) {
		//},
59
		TraverseChildren: true,
Heiko Reese's avatar
 
Heiko Reese committed
60
	}
61
	// see also: Zertifizierungsrichtlinie der DFN-PKI-Sicherheitsniveau Global, Version:3.8, (OID): 1.3.6.1.4.1.22177.300.1.1.4.3.8
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
	cmdArguments struct {
		CAName string
		RAId   int

		Profile        string
		CommonName     string
		Organisation   string
		OU             []string
		Locality       string
		State          string
		Country        string
		DNS            []string
		IP             IPSLice
		URI            URLSlice
		Email          []string
		RequesterName  string
		RequesterEmail string
		RequesterOU    string
		Keyfile        string
		Keypass        string
		Keysize        int
		PIN            string
		Outdir         string
		SkipPDF        bool
		Publish        bool
		DryRun         bool
Heiko Reese's avatar
 
Heiko Reese committed
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
	}
)

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
	if err := rootCmd.Execute(); err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
}

func init() {
	cobra.OnInitialize(initConfig)

Heiko Reese's avatar
   
Heiko Reese committed
103
	rootCmd.Flags().SortFlags = false
Heiko Reese's avatar
Typos    
Heiko Reese committed
104
	rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.config/dfnpkitool.{toml,yaml,json})")
105
106
	rootCmd.PersistentFlags().StringVarP(&cmdArguments.CAName, "ca", "C", "", "Certification Authority identifier")
	rootCmd.PersistentFlags().IntVarP(&cmdArguments.RAId, "ra-id", "R", 0, "Sub-RA identifier (usually 0 for main RA)")
Heiko Reese's avatar
   
Heiko Reese committed
107

108
109
110
	//viper.BindPFlag("ca", rootCmd.PersistentFlags().Lookup("ca"))
	//viper.BindPFlag("raid", rootCmd.PersistentFlags().Lookup("ra-id"))
	viper.BindPFlags(rootCmd.PersistentFlags())
Heiko Reese's avatar
 
Heiko Reese committed
111
112
113
114
115
116
117
118
119
120
121
}

// initConfig reads in config file and ENV variables if set.
func initConfig() {
	if cfgFile != "" {
		// Use config file from the flag.
		viper.SetConfigFile(cfgFile)
	} else {
		// Find home directory.
		home, err := homedir.Dir()
		if err != nil {
Heiko Reese's avatar
   
Heiko Reese committed
122
			log.Fatal(err)
Heiko Reese's avatar
 
Heiko Reese committed
123
124
125
126
127
128
129
130
131
132
133
		}

		// Search config in home directory with name "dfnpkitool" (without extension).
		viper.SetConfigName("dfnpkitool")
		viper.AddConfigPath(filepath.Join(home, configsubdir))
		viper.AddConfigPath(".")
	}

	// If a config file is found, read it in.
	err := viper.ReadInConfig()
	if err == nil {
Heiko Reese's avatar
   
Heiko Reese committed
134
		log.Println("Using config file:", viper.ConfigFileUsed())
Heiko Reese's avatar
 
Heiko Reese committed
135
136
	}
}