rename hyperclair to clairctl in config and test

This commit is contained in:
jgsqware 2016-05-17 18:31:53 +02:00
parent afd0d46d04
commit edd36db8dc
10 changed files with 62 additions and 62 deletions

View File

@ -36,7 +36,7 @@ Available Commands:
version Get Versions of clairctl and underlying services version Get Versions of clairctl and underlying services
Flags: Flags:
--config string config file (default is ./.hyperclair.yml) --config string config file (default is ./.clairctl.yml)
--log-level string log level [Panic,Fatal,Error,Warn,Info,Debug] --log-level string log level [Panic,Fatal,Error,Warn,Info,Debug]
Use "clairctl [command] --help" for more information about a command. Use "clairctl [command] --help" for more information about a command.

View File

@ -36,7 +36,7 @@ var loginCmd = &cobra.Command{
var users userMapping var users userMapping
if err := readConfigFile(&users, config.HyperclairConfig()); err != nil { if err := readConfigFile(&users, config.ClairctlConfig()); err != nil {
fmt.Println(errInternalError) fmt.Println(errInternalError)
logrus.Fatalf("reading clairctl file: %v", err) logrus.Fatalf("reading clairctl file: %v", err)
} }
@ -55,7 +55,7 @@ var loginCmd = &cobra.Command{
users[reg] = usr users[reg] = usr
if err := writeConfigFile(users, config.HyperclairConfig()); err != nil { if err := writeConfigFile(users, config.ClairctlConfig()); err != nil {
fmt.Println(errInternalError) fmt.Println(errInternalError)
logrus.Fatalf("indenting login: %v", err) logrus.Fatalf("indenting login: %v", err)
} }

View File

@ -26,17 +26,17 @@ var logoutCmd = &cobra.Command{
reg = args[0] reg = args[0]
} }
if _, err := os.Stat(config.HyperclairConfig()); err == nil { if _, err := os.Stat(config.ClairctlConfig()); err == nil {
var users userMapping var users userMapping
if err := readConfigFile(&users, config.HyperclairConfig()); err != nil { if err := readConfigFile(&users, config.ClairctlConfig()); err != nil {
fmt.Println(errInternalError) fmt.Println(errInternalError)
logrus.Fatalf("reading clairctl file: %v", err) logrus.Fatalf("reading clairctl file: %v", err)
} }
if _, present := users[reg]; present { if _, present := users[reg]; present {
delete(users, reg) delete(users, reg)
if err := writeConfigFile(users, config.HyperclairConfig()); err != nil { if err := writeConfigFile(users, config.ClairctlConfig()); err != nil {
fmt.Println(errInternalError) fmt.Println(errInternalError)
logrus.Fatalf("indenting login: %v", err) logrus.Fatalf("indenting login: %v", err)
} }

View File

@ -50,7 +50,7 @@ func Execute() {
func init() { func init() {
cobra.OnInitialize(initConfig) cobra.OnInitialize(initConfig)
RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.hyperclair.yml)") RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.clairctl.yml)")
RootCmd.PersistentFlags().StringVar(&logLevel, "log-level", "", "log level [Panic,Fatal,Error,Warn,Info,Debug]") RootCmd.PersistentFlags().StringVar(&logLevel, "log-level", "", "log level [Panic,Fatal,Error,Warn,Info,Debug]")
} }

View File

@ -23,25 +23,25 @@ import (
var ErrLoginNotFound = errors.New("user is not log in") var ErrLoginNotFound = errors.New("user is not log in")
type r struct { type reportConfig struct {
Path, Format string Path, Format string
} }
type c struct { type clairConfig struct {
URI, Priority string URI, Priority string
Port, HealthPort int Port, HealthPort int
Report r Report reportConfig
} }
type a struct { type authConfig struct {
InsecureSkipVerify bool InsecureSkipVerify bool
} }
type h struct { type clairctlConfig struct {
IP, TempFolder string IP, TempFolder string
Port int Port int
} }
type config struct { type config struct {
Clair c Clair clairConfig
Auth a Auth authConfig
Hyperclair h Clairctl clairctlConfig
} }
// Init reads in config file and ENV variables if set. // Init reads in config file and ENV variables if set.
@ -57,11 +57,11 @@ func Init(cfgFile string, logLevel string) {
} }
logrus.SetLevel(lvl) logrus.SetLevel(lvl)
viper.SetEnvPrefix("hyperclair") viper.SetEnvPrefix("clairctl")
viper.SetConfigName("hyperclair") // name of config file (without extension) viper.SetConfigName("clairctl") // name of config file (without extension)
viper.AddConfigPath("$HOME/.hyperclair") // adding home directory as first search path viper.AddConfigPath("$HOME/.clairctl") // adding home directory as first search path
viper.AddConfigPath(".") // adding home directory as first search path viper.AddConfigPath(".") // adding home directory as first search path
viper.AutomaticEnv() // read in environment variables that match viper.AutomaticEnv() // read in environment variables that match
if cfgFile != "" { if cfgFile != "" {
viper.SetConfigFile(cfgFile) viper.SetConfigFile(cfgFile)
} }
@ -93,37 +93,37 @@ func Init(cfgFile string, logLevel string) {
if viper.Get("auth.insecureSkipVerify") == nil { if viper.Get("auth.insecureSkipVerify") == nil {
viper.Set("auth.insecureSkipVerify", "true") viper.Set("auth.insecureSkipVerify", "true")
} }
if viper.Get("hyperclair.ip") == nil { if viper.Get("clairctl.ip") == nil {
viper.Set("hyperclair.ip", "") viper.Set("clairctl.ip", "")
} }
if viper.Get("hyperclair.port") == nil { if viper.Get("clairctl.port") == nil {
viper.Set("hyperclair.port", 0) viper.Set("clairctl.port", 0)
} }
if viper.Get("hyperclair.tempFolder") == nil { if viper.Get("clairctl.tempFolder") == nil {
viper.Set("hyperclair.tempFolder", "/tmp/hyperclair") viper.Set("clairctl.tempFolder", "/tmp/clairctl")
} }
clair.Config() clair.Config()
} }
func values() config { func values() config {
return config{ return config{
Clair: c{ Clair: clairConfig{
URI: viper.GetString("clair.uri"), URI: viper.GetString("clair.uri"),
Port: viper.GetInt("clair.port"), Port: viper.GetInt("clair.port"),
HealthPort: viper.GetInt("clair.healthPort"), HealthPort: viper.GetInt("clair.healthPort"),
Priority: viper.GetString("clair.priority"), Priority: viper.GetString("clair.priority"),
Report: r{ Report: reportConfig{
Path: viper.GetString("clair.report.path"), Path: viper.GetString("clair.report.path"),
Format: viper.GetString("clair.report.format"), Format: viper.GetString("clair.report.format"),
}, },
}, },
Auth: a{ Auth: authConfig{
InsecureSkipVerify: viper.GetBool("auth.insecureSkipVerify"), InsecureSkipVerify: viper.GetBool("auth.insecureSkipVerify"),
}, },
Hyperclair: h{ Clairctl: clairctlConfig{
IP: viper.GetString("hyperclair.ip"), IP: viper.GetString("clairctl.ip"),
Port: viper.GetInt("hyperclair.port"), Port: viper.GetInt("clairctl.port"),
TempFolder: viper.GetString("hyperclair.tempFolder"), TempFolder: viper.GetString("clairctl.tempFolder"),
}, },
} }
} }
@ -139,12 +139,12 @@ func Print() {
fmt.Printf("%v", string(cfgBytes)) fmt.Printf("%v", string(cfgBytes))
} }
func HyperclairHome() string { func ClairctlHome() string {
usr, err := user.Current() usr, err := user.Current()
if err != nil { if err != nil {
panic(err) panic(err)
} }
p := usr.HomeDir + "/.hyperclair" p := usr.HomeDir + "/.clairctl"
if _, err := os.Stat(p); os.IsNotExist(err) { if _, err := os.Stat(p); os.IsNotExist(err) {
os.Mkdir(p, 0700) os.Mkdir(p, 0700)
@ -159,31 +159,31 @@ type Login struct {
type loginMapping map[string]Login type loginMapping map[string]Login
func HyperclairConfig() string { func ClairctlConfig() string {
return HyperclairHome() + "/config.json" return ClairctlHome() + "/config.json"
} }
func AddLogin(registry string, login Login) error { func AddLogin(registry string, login Login) error {
var logins loginMapping var logins loginMapping
if err := readConfigFile(&logins, HyperclairConfig()); err != nil { if err := readConfigFile(&logins, ClairctlConfig()); err != nil {
return fmt.Errorf("reading hyperclair file: %v", err) return fmt.Errorf("reading clairctl file: %v", err)
} }
logins[registry] = login logins[registry] = login
if err := writeConfigFile(logins, HyperclairConfig()); err != nil { if err := writeConfigFile(logins, ClairctlConfig()); err != nil {
return fmt.Errorf("indenting login: %v", err) return fmt.Errorf("indenting login: %v", err)
} }
return nil return nil
} }
func GetLogin(registry string) (Login, error) { func GetLogin(registry string) (Login, error) {
if _, err := os.Stat(HyperclairConfig()); err == nil { if _, err := os.Stat(ClairctlConfig()); err == nil {
var logins loginMapping var logins loginMapping
if err := readConfigFile(&logins, HyperclairConfig()); err != nil { if err := readConfigFile(&logins, ClairctlConfig()); err != nil {
return Login{}, fmt.Errorf("reading hyperclair file: %v", err) return Login{}, fmt.Errorf("reading clairctl file: %v", err)
} }
if login, present := logins[registry]; present { if login, present := logins[registry]; present {
@ -199,17 +199,17 @@ func GetLogin(registry string) (Login, error) {
} }
func RemoveLogin(registry string) (bool, error) { func RemoveLogin(registry string) (bool, error) {
if _, err := os.Stat(HyperclairConfig()); err == nil { if _, err := os.Stat(ClairctlConfig()); err == nil {
var logins loginMapping var logins loginMapping
if err := readConfigFile(&logins, HyperclairConfig()); err != nil { if err := readConfigFile(&logins, ClairctlConfig()); err != nil {
return false, fmt.Errorf("reading hyperclair file: %v", err) return false, fmt.Errorf("reading clairctl file: %v", err)
} }
if _, present := logins[registry]; present { if _, present := logins[registry]; present {
delete(logins, registry) delete(logins, registry)
if err := writeConfigFile(logins, HyperclairConfig()); err != nil { if err := writeConfigFile(logins, ClairctlConfig()); err != nil {
return false, fmt.Errorf("indenting login: %v", err) return false, fmt.Errorf("indenting login: %v", err)
} }
@ -247,10 +247,10 @@ func writeConfigFile(logins loginMapping, file string) error {
return nil return nil
} }
//LocalServerIP return the local hyperclair server IP //LocalServerIP return the local clairctl server IP
func LocalServerIP() (string, error) { func LocalServerIP() (string, error) {
localPort := viper.GetString("hyperclair.port") localPort := viper.GetString("clairctl.port")
localIP := viper.GetString("hyperclair.ip") localIP := viper.GetString("clairctl.ip")
if localIP == "" { if localIP == "" {
logrus.Infoln("retrieving docker0 interface as local IP") logrus.Infoln("retrieving docker0 interface as local IP")
var err error var err error

View File

@ -5,8 +5,8 @@ import (
"os" "os"
"testing" "testing"
"github.com/spf13/viper"
"github.com/coreos/clair/cmd/clairctl/test" "github.com/coreos/clair/cmd/clairctl/test"
"github.com/spf13/viper"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
) )
@ -22,9 +22,9 @@ clair:
format: html format: html
auth: auth:
insecureskipverify: true insecureskipverify: true
hyperclair: clairctl:
ip: "" ip: ""
tempfolder: /tmp/hyperclair tempfolder: /tmp/clairctl
port: 0 port: 0
` `
@ -39,9 +39,9 @@ clair:
format: json format: json
auth: auth:
insecureskipverify: false insecureskipverify: false
hyperclair: clairctl:
ip: "localhost" ip: "localhost"
tempfolder: /tmp/hyperclair/test tempfolder: /tmp/clairctl/test
port: 64157 port: 64157
` `
@ -63,7 +63,7 @@ func TestInitDefault(t *testing.T) {
} }
func TestInitCustomLocal(t *testing.T) { func TestInitCustomLocal(t *testing.T) {
tmpfile := test.CreateConfigFile(customValues, "hyperclair.yml", ".") tmpfile := test.CreateConfigFile(customValues, "clairctl.yml", ".")
defer os.Remove(tmpfile) // clean up defer os.Remove(tmpfile) // clean up
fmt.Println(tmpfile) fmt.Println(tmpfile)
Init("", "INFO") Init("", "INFO")
@ -83,7 +83,7 @@ func TestInitCustomLocal(t *testing.T) {
} }
func TestInitCustomHome(t *testing.T) { func TestInitCustomHome(t *testing.T) {
tmpfile := test.CreateConfigFile(customValues, "hyperclair.yml", HyperclairHome()) tmpfile := test.CreateConfigFile(customValues, "clairctl.yml", ClairctlHome())
defer os.Remove(tmpfile) // clean up defer os.Remove(tmpfile) // clean up
fmt.Println(tmpfile) fmt.Println(tmpfile)
Init("", "INFO") Init("", "INFO")
@ -103,7 +103,7 @@ func TestInitCustomHome(t *testing.T) {
} }
func TestInitCustom(t *testing.T) { func TestInitCustom(t *testing.T) {
tmpfile := test.CreateConfigFile(customValues, "hyperclair.yml", "/tmp") tmpfile := test.CreateConfigFile(customValues, "clairctl.yml", "/tmp")
defer os.Remove(tmpfile) // clean up defer os.Remove(tmpfile) // clean up
fmt.Println(tmpfile) fmt.Println(tmpfile)
Init(tmpfile, "INFO") Init(tmpfile, "INFO")

View File

@ -33,7 +33,7 @@ const hubURI = "https://" + DockerHub + "/v2"
var IsLocal = false var IsLocal = false
func TmpLocal() string { func TmpLocal() string {
return viper.GetString("hyperclair.tempFolder") return viper.GetString("clairctl.tempFolder")
} }
// Parse is used to parse a docker image command // Parse is used to parse a docker image command

View File

@ -41,10 +41,10 @@ func tcpListener(sURL string) (listener net.Listener) {
logrus.Fatalf("cannot instanciate listener: %v", err) logrus.Fatalf("cannot instanciate listener: %v", err)
} }
if viper.GetInt("hyperclair.port") == 0 { if viper.GetInt("clairctl.port") == 0 {
port := strings.Split(listener.Addr().String(), ":")[1] port := strings.Split(listener.Addr().String(), ":")[1]
logrus.Debugf("Update local server port from %q to %q", "0", port) logrus.Debugf("Update local server port from %q to %q", "0", port)
viper.Set("hyperclair.port", port) viper.Set("clairctl.port", port)
} }
return return

View File

@ -9,7 +9,7 @@ import (
func CreateTmpConfigFile(content string) string { func CreateTmpConfigFile(content string) string {
c := []byte(content) c := []byte(content)
tmpfile, err := ioutil.TempFile("", "test-hyperclair") tmpfile, err := ioutil.TempFile("", "test-clairctl")
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }