| 1 |
// Copyright © 2015 Michael Wagner <[email protected]> |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
package main |
| 16 |
|
| 17 |
import ( |
| 18 |
"fmt" |
| 19 |
"os" |
| 20 |
"path/filepath" |
| 21 |
"strings" |
| 22 |
|
| 23 |
"github.com/mitch000001/go-hbci/client" |
| 24 |
"github.com/mitch000001/go-hbci/domain" |
| 25 |
"github.com/mitch000001/go-hbci/internal" |
| 26 |
homedir "github.com/mitchellh/go-homedir" |
| 27 |
"github.com/spf13/cobra" |
| 28 |
"github.com/spf13/viper" |
| 29 |
) |
| 30 |
|
| 31 |
// rootCmd represents the base command when called without any subcommands |
| 32 |
var rootCmd = &cobra.Command{ |
| 33 |
Use: "banking", |
| 34 |
Short: "banking is a CLI to access bank data via HBCI", |
| 35 |
Long: `banking is a CLI to access bank data via HBCI`, |
| 36 |
// Uncomment the following line if your bare application |
| 37 |
// has an action associated with it: |
| 38 |
// Run: func(cmd *cobra.Command, args []string) { }, |
| 39 |
} |
| 40 |
|
| 41 |
// Execute adds all child commands to the root command and sets flags appropriately. |
| 42 |
// This is called by main.main(). It only needs to happen once to the rootCmd. |
| 43 |
func execute() { |
| 44 |
if err := rootCmd.Execute(); err != nil { |
| 45 |
fmt.Println(err) |
| 46 |
os.Exit(1) |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
var cfgFile string |
| 51 |
var url string |
| 52 |
var UserID string |
| 53 |
var BLZ string |
| 54 |
var PIN string |
| 55 |
|
| 56 |
var account domain.InternationalAccountConnection |
| 57 |
var clientConfig client.Config |
| 58 |
var hbciClient *client.Client |
| 59 |
var debug bool |
| 60 |
|
| 61 |
func init() { |
| 62 |
cobra.OnInitialize( |
| 63 |
initConfig, |
| 64 |
initClient, |
| 65 |
initLoggers, |
| 66 |
) |
| 67 |
|
| 68 |
// Here you will define your flags and configuration settings. |
| 69 |
// Cobra supports persistent flags, which, if defined here, |
| 70 |
// will be global for your application. |
| 71 |
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.banking.yaml)") |
| 72 |
rootCmd.PersistentFlags().StringVar(&url, "hbci.url", "", "the URL to the bank institute") |
| 73 |
rootCmd.PersistentFlags().StringVar(&UserID, "userID", "", "the account ID to authenticate with") |
| 74 |
rootCmd.PersistentFlags().StringVar(&BLZ, "blz", "", "the identifier for the bank institute") |
| 75 |
rootCmd.PersistentFlags().StringVar(&PIN, "pin", "", "the pin for the provided account") |
| 76 |
viper.BindPFlag("userID", rootCmd.PersistentFlags().Lookup("userID")) |
| 77 |
viper.BindPFlag("blz", rootCmd.PersistentFlags().Lookup("blz")) |
| 78 |
rootCmd.MarkPersistentFlagRequired("pin") |
| 79 |
|
| 80 |
rootCmd.PersistentFlags().BoolVarP(&debug, "debug", "d", false, "enable debug logging (very verbose)") |
| 81 |
} |
| 82 |
|
| 83 |
func initClient() { |
| 84 |
var missingFlags []string |
| 85 |
userID := viper.GetString("userID") |
| 86 |
blz := viper.GetString("blz") |
| 87 |
if userID == "" { |
| 88 |
missingFlags = append(missingFlags, `"userID"`) |
| 89 |
} |
| 90 |
if blz == "" { |
| 91 |
missingFlags = append(missingFlags, `"blz"`) |
| 92 |
} |
| 93 |
if len(missingFlags) != 0 { |
| 94 |
fmt.Printf("Error: required flag(s) %s not set\n", strings.Join(missingFlags, ", ")) |
| 95 |
os.Exit(1) |
| 96 |
} |
| 97 |
clientConfig = client.Config{ |
| 98 |
URL: url, |
| 99 |
AccountID: userID, |
| 100 |
BankID: blz, |
| 101 |
PIN: PIN, |
| 102 |
} |
| 103 |
c, err := client.New(clientConfig) |
| 104 |
if err != nil { |
| 105 |
fmt.Println(err) |
| 106 |
os.Exit(1) |
| 107 |
} |
| 108 |
hbciClient = c |
| 109 |
} |
| 110 |
|
| 111 |
// initConfig reads in config file and ENV variables if set. |
| 112 |
func initConfig() { |
| 113 |
if cfgFile != "" { |
| 114 |
// Use config file from the flag. |
| 115 |
viper.SetConfigFile(cfgFile) |
| 116 |
} else { |
| 117 |
// Find home directory. |
| 118 |
home, err := homedir.Dir() |
| 119 |
if err != nil { |
| 120 |
fmt.Println(err) |
| 121 |
os.Exit(1) |
| 122 |
} |
| 123 |
|
| 124 |
// Search config in home directory with name ".banking" (without extension). |
| 125 |
viper.AddConfigPath(home) |
| 126 |
viper.SetConfigName(".banking") |
| 127 |
} |
| 128 |
|
| 129 |
viper.AutomaticEnv() // read in environment variables that match |
| 130 |
|
| 131 |
// If a config file is found, read it in. |
| 132 |
if err := viper.ReadInConfig(); err == nil { |
| 133 |
fmt.Println("Using config file:", viper.ConfigFileUsed()) |
| 134 |
} else { |
| 135 |
// Find home directory. |
| 136 |
home, err := homedir.Dir() |
| 137 |
if err != nil { |
| 138 |
fmt.Println(err) |
| 139 |
os.Exit(1) |
| 140 |
} |
| 141 |
viper.WriteConfigAs(filepath.Join(home, ".banking.yaml")) |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
func initLoggers() { |
| 146 |
internal.SetDebugMode(debug) |
| 147 |
} |