Relicensed under MIT.

This commit is contained in:
Stanislav N. aka pztrn 2018-03-23 08:14:25 +05:00
parent c9b4726e17
commit 0754a665e1
8 changed files with 236 additions and 209 deletions

View File

@ -1,8 +0,0 @@
stages:
- test
test_job:
stage: test
tags:
- linux1
script: go test -test.v .

3
.travis.yml Normal file
View File

@ -0,0 +1,3 @@
language: go
go:
- "1.10"

7
LICENSE Normal file
View File

@ -0,0 +1,7 @@
Copyright 2017-2018, Stanislav N. aka pztrn
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View File

@ -1,29 +1,34 @@
// Flagger - arbitrary CLI flags parser. // Flagger - arbitrary CLI flags parser.
// //
// Copyright (c) 2017, Stanislav N. aka pztrn. // Copyright (c) 2017-2018, Stanislav N. aka pztrn.
// All rights reserved.
// //
// This program is free software: you can redistribute it and/or modify // Permission is hereby granted, free of charge, to any person obtaining
// it under the terms of the GNU General Public License as published by // a copy of this software and associated documentation files (the
// the Free Software Foundation, either version 3 of the License, or // "Software"), to deal in the Software without restriction, including
// (at your option) any later version. // without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject
// to the following conditions:
// //
// This program is distributed in the hope that it will be useful, // The above copyright notice and this permission notice shall be
// but WITHOUT ANY WARRANTY; without even the implied warranty of // included in all copies or substantial portions of the Software.
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// //
// You should have received a copy of the GNU General Public License // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// along with this program. If not, see <http://www.gnu.org/licenses/>. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
// OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package flagger package flagger
var ( var (
logger LoggerInterface logger LoggerInterface
) )
func New(l LoggerInterface) *Flagger { func New(l LoggerInterface) *Flagger {
logger = l logger = l
f := Flagger{} f := Flagger{}
return &f return &f
} }

45
flag.go
View File

@ -1,31 +1,36 @@
// Flagger - arbitrary CLI flags parser. // Flagger - arbitrary CLI flags parser.
// //
// Copyright (c) 2017, Stanislav N. aka pztrn. // Copyright (c) 2017-2018, Stanislav N. aka pztrn.
// All rights reserved.
// //
// This program is free software: you can redistribute it and/or modify // Permission is hereby granted, free of charge, to any person obtaining
// it under the terms of the GNU General Public License as published by // a copy of this software and associated documentation files (the
// the Free Software Foundation, either version 3 of the License, or // "Software"), to deal in the Software without restriction, including
// (at your option) any later version. // without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject
// to the following conditions:
// //
// This program is distributed in the hope that it will be useful, // The above copyright notice and this permission notice shall be
// but WITHOUT ANY WARRANTY; without even the implied warranty of // included in all copies or substantial portions of the Software.
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// //
// You should have received a copy of the GNU General Public License // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// along with this program. If not, see <http://www.gnu.org/licenses/>. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
// OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package flagger package flagger
// This structure represents addable flag for Flagger. // This structure represents addable flag for Flagger.
type Flag struct { type Flag struct {
// Flag name. It will be accessible using this name later. // Flag name. It will be accessible using this name later.
Name string Name string
// Description for help output. // Description for help output.
Description string Description string
// Type can be one of "bool", "int", "string". // Type can be one of "bool", "int", "string".
Type string Type string
// This value will be reflected. // This value will be reflected.
DefaultValue interface{} DefaultValue interface{}
} }

View File

@ -1,28 +1,33 @@
// Flagger - arbitrary CLI flags parser. // Flagger - arbitrary CLI flags parser.
// //
// Copyright (c) 2017, Stanislav N. aka pztrn. // Copyright (c) 2017-2018, Stanislav N. aka pztrn.
// All rights reserved.
// //
// This program is free software: you can redistribute it and/or modify // Permission is hereby granted, free of charge, to any person obtaining
// it under the terms of the GNU General Public License as published by // a copy of this software and associated documentation files (the
// the Free Software Foundation, either version 3 of the License, or // "Software"), to deal in the Software without restriction, including
// (at your option) any later version. // without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject
// to the following conditions:
// //
// This program is distributed in the hope that it will be useful, // The above copyright notice and this permission notice shall be
// but WITHOUT ANY WARRANTY; without even the implied warranty of // included in all copies or substantial portions of the Software.
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// //
// You should have received a copy of the GNU General Public License // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// along with this program. If not, see <http://www.gnu.org/licenses/>. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
// OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package flagger package flagger
import ( import (
// stdlib // stdlib
"errors" "errors"
"flag" "flag"
"sync" "sync"
) )
// Flagger implements (kinda) extended CLI parameters parser. As it // Flagger implements (kinda) extended CLI parameters parser. As it
@ -32,90 +37,90 @@ import (
// It uses reflection to determine what kind of variable we should // It uses reflection to determine what kind of variable we should
// parse or get. // parse or get.
type Flagger struct { type Flagger struct {
// Flags that was added by user. // Flags that was added by user.
flags map[string]*Flag flags map[string]*Flag
flagsMutex sync.Mutex flagsMutex sync.Mutex
// Flags that will be passed to flag module. // Flags that will be passed to flag module.
flagsBool map[string]*bool flagsBool map[string]*bool
flagsInt map[string]*int flagsInt map[string]*int
flagsString map[string]*string flagsString map[string]*string
} }
// Adds flag to list of flags we will pass to ``flag`` package. // Adds flag to list of flags we will pass to ``flag`` package.
func (f *Flagger) AddFlag(flag *Flag) error { func (f *Flagger) AddFlag(flag *Flag) error {
_, present := f.flags[flag.Name] _, present := f.flags[flag.Name]
if present { if present {
logger.Fatalln("Cannot add flag '" + flag.Name + "' - already added!") logger.Fatalln("Cannot add flag '" + flag.Name + "' - already added!")
return errors.New("Cannot add flag '" + flag.Name + "' - already added!") return errors.New("Cannot add flag '" + flag.Name + "' - already added!")
} }
f.flags[flag.Name] = flag f.flags[flag.Name] = flag
return nil return nil
} }
// This function returns boolean value for flag with given name. // This function returns boolean value for flag with given name.
// Returns bool value for flag and nil as error on success // Returns bool value for flag and nil as error on success
// and false bool plus error with text on error. // and false bool plus error with text on error.
func (f *Flagger) GetBoolValue(name string) (bool, error) { func (f *Flagger) GetBoolValue(name string) (bool, error) {
fl, present := f.flagsBool[name] fl, present := f.flagsBool[name]
if !present { if !present {
return false, errors.New("No such flag: " + name) return false, errors.New("No such flag: " + name)
} }
return (*fl), nil return (*fl), nil
} }
// This function returns integer value for flag with given name. // This function returns integer value for flag with given name.
// Returns integer on success and 0 on error. // Returns integer on success and 0 on error.
func (f *Flagger) GetIntValue(name string) (int, error) { func (f *Flagger) GetIntValue(name string) (int, error) {
fl, present := f.flagsInt[name] fl, present := f.flagsInt[name]
if !present { if !present {
return 0, errors.New("No such flag: " + name) return 0, errors.New("No such flag: " + name)
} }
return (*fl), nil return (*fl), nil
} }
// This function returns string value for flag with given name. // This function returns string value for flag with given name.
// Returns string on success or empty string on error. // Returns string on success or empty string on error.
func (f *Flagger) GetStringValue(name string) (string, error) { func (f *Flagger) GetStringValue(name string) (string, error) {
fl, present := f.flagsString[name] fl, present := f.flagsString[name]
if !present { if !present {
return "", errors.New("No such flag: " + name) return "", errors.New("No such flag: " + name)
} }
return (*fl), nil return (*fl), nil
} }
// Flagger initialization. // Flagger initialization.
func (f *Flagger) Initialize() { func (f *Flagger) Initialize() {
logger.Println("Initializing CLI parameters parser...") logger.Println("Initializing CLI parameters parser...")
f.flags = make(map[string]*Flag) f.flags = make(map[string]*Flag)
f.flagsBool = make(map[string]*bool) f.flagsBool = make(map[string]*bool)
f.flagsInt = make(map[string]*int) f.flagsInt = make(map[string]*int)
f.flagsString = make(map[string]*string) f.flagsString = make(map[string]*string)
} }
// This function adds flags from flags map to flag package and parse // This function adds flags from flags map to flag package and parse
// them. They can be obtained later by calling GetTYPEValue(name), // them. They can be obtained later by calling GetTYPEValue(name),
// where TYPE is one of Bool, Int, String. // where TYPE is one of Bool, Int, String.
func (f *Flagger) Parse() { func (f *Flagger) Parse() {
for name, fl := range f.flags { for name, fl := range f.flags {
if fl.Type == "bool" { if fl.Type == "bool" {
fdef := fl.DefaultValue.(bool) fdef := fl.DefaultValue.(bool)
f.flagsBool[name] = &fdef f.flagsBool[name] = &fdef
flag.BoolVar(&fdef, name, fdef, fl.Description) flag.BoolVar(&fdef, name, fdef, fl.Description)
} else if fl.Type == "int" { } else if fl.Type == "int" {
fdef := fl.DefaultValue.(int) fdef := fl.DefaultValue.(int)
f.flagsInt[name] = &fdef f.flagsInt[name] = &fdef
flag.IntVar(&fdef, name, fdef, fl.Description) flag.IntVar(&fdef, name, fdef, fl.Description)
} else if fl.Type == "string" { } else if fl.Type == "string" {
fdef := fl.DefaultValue.(string) fdef := fl.DefaultValue.(string)
f.flagsString[name] = &fdef f.flagsString[name] = &fdef
flag.StringVar(&fdef, name, fdef, fl.Description) flag.StringVar(&fdef, name, fdef, fl.Description)
} }
} }
logger.Println("Parsing CLI parameters...") logger.Println("Parsing CLI parameters...")
flag.Parse() flag.Parse()
} }

View File

@ -1,125 +1,130 @@
// Flagger - arbitrary CLI flags parser. // Flagger - arbitrary CLI flags parser.
// //
// Copyright (c) 2017, Stanislav N. aka pztrn. // Copyright (c) 2017-2018, Stanislav N. aka pztrn.
// All rights reserved.
// //
// This program is free software: you can redistribute it and/or modify // Permission is hereby granted, free of charge, to any person obtaining
// it under the terms of the GNU General Public License as published by // a copy of this software and associated documentation files (the
// the Free Software Foundation, either version 3 of the License, or // "Software"), to deal in the Software without restriction, including
// (at your option) any later version. // without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject
// to the following conditions:
// //
// This program is distributed in the hope that it will be useful, // The above copyright notice and this permission notice shall be
// but WITHOUT ANY WARRANTY; without even the implied warranty of // included in all copies or substantial portions of the Software.
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// //
// You should have received a copy of the GNU General Public License // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// along with this program. If not, see <http://www.gnu.org/licenses/>. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
// OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package flagger package flagger
import ( import (
// stdlib // stdlib
"log" "log"
"os" "os"
"testing" "testing"
) )
var ( var (
f *Flagger f *Flagger
) )
func TestFlaggerInitialization(t *testing.T) { func TestFlaggerInitialization(t *testing.T) {
f = New(LoggerInterface(log.New(os.Stdout, "testing logger: ", log.Lshortfile))) f = New(LoggerInterface(log.New(os.Stdout, "testing logger: ", log.Lshortfile)))
if f == nil { if f == nil {
t.Fatal("Logger initialization failed!") t.Fatal("Logger initialization failed!")
t.FailNow() t.FailNow()
} }
f.Initialize() f.Initialize()
} }
func TestFlaggerAddBoolFlag(t *testing.T) { func TestFlaggerAddBoolFlag(t *testing.T) {
flag_testbool := Flag{ flag_testbool := Flag{
Name: "testboolflag", Name: "testboolflag",
Description: "Testing boolean flag", Description: "Testing boolean flag",
Type: "bool", Type: "bool",
DefaultValue: true, DefaultValue: true,
} }
err := f.AddFlag(&flag_testbool) err := f.AddFlag(&flag_testbool)
if err != nil { if err != nil {
t.Fatal("Failed to add boolean flag!") t.Fatal("Failed to add boolean flag!")
t.FailNow() t.FailNow()
} }
} }
func TestFlaggerAddIntFlag(t *testing.T) { func TestFlaggerAddIntFlag(t *testing.T) {
flag_testint := Flag{ flag_testint := Flag{
Name: "testintflag", Name: "testintflag",
Description: "Testing integer flag", Description: "Testing integer flag",
Type: "int", Type: "int",
DefaultValue: 1, DefaultValue: 1,
} }
err := f.AddFlag(&flag_testint) err := f.AddFlag(&flag_testint)
if err != nil { if err != nil {
t.Fatal("Failed to add integer flag!") t.Fatal("Failed to add integer flag!")
t.FailNow() t.FailNow()
} }
} }
func TestFlaggerAddStringFlag(t *testing.T) { func TestFlaggerAddStringFlag(t *testing.T) {
flag_teststring := Flag{ flag_teststring := Flag{
Name: "teststringflag", Name: "teststringflag",
Description: "Testing string flag", Description: "Testing string flag",
Type: "string", Type: "string",
DefaultValue: "superstring", DefaultValue: "superstring",
} }
err := f.AddFlag(&flag_teststring) err := f.AddFlag(&flag_teststring)
if err != nil { if err != nil {
t.Fatal("Failed to add string flag!") t.Fatal("Failed to add string flag!")
t.FailNow() t.FailNow()
} }
} }
// This test doing nothing more but launching flags parsing. // This test doing nothing more but launching flags parsing.
func TestFlaggerParse(t *testing.T) { func TestFlaggerParse(t *testing.T) {
f.Parse() f.Parse()
} }
func TestFlaggerGetBoolFlag(t *testing.T) { func TestFlaggerGetBoolFlag(t *testing.T) {
val, err := f.GetBoolValue("testboolflag") val, err := f.GetBoolValue("testboolflag")
if err != nil { if err != nil {
t.Fatal("Failed to get boolean flag: " + err.Error()) t.Fatal("Failed to get boolean flag: " + err.Error())
t.FailNow() t.FailNow()
} }
if !val { if !val {
t.Fatal("Failed to get boolean flag - should be true, but false received") t.Fatal("Failed to get boolean flag - should be true, but false received")
t.FailNow() t.FailNow()
} }
} }
func TestFlaggerGetIntFlag(t *testing.T) { func TestFlaggerGetIntFlag(t *testing.T) {
val, err := f.GetIntValue("testintflag") val, err := f.GetIntValue("testintflag")
if err != nil { if err != nil {
t.Fatal("Failed to get integer flag: " + err.Error()) t.Fatal("Failed to get integer flag: " + err.Error())
t.FailNow() t.FailNow()
} }
if val == 0 { if val == 0 {
t.Fatal("Failed to get integer flag - should be 1, but 0 received") t.Fatal("Failed to get integer flag - should be 1, but 0 received")
t.FailNow() t.FailNow()
} }
} }
func TestFlaggerGetStringFlag(t *testing.T) { func TestFlaggerGetStringFlag(t *testing.T) {
val, err := f.GetStringValue("teststringflag") val, err := f.GetStringValue("teststringflag")
if err != nil { if err != nil {
t.Fatal("Failed to get string flag: " + err.Error()) t.Fatal("Failed to get string flag: " + err.Error())
t.FailNow() t.FailNow()
} }
if val == "" { if val == "" {
t.Fatal("Failed to get string flag - should be 'superstring', but nothing received") t.Fatal("Failed to get string flag - should be 'superstring', but nothing received")
t.FailNow() t.FailNow()
} }
} }

View File

@ -1,26 +1,31 @@
// Flagger - arbitrary CLI flags parser. // Flagger - arbitrary CLI flags parser.
// //
// Copyright (c) 2017, Stanislav N. aka pztrn. // Copyright (c) 2017-2018, Stanislav N. aka pztrn.
// All rights reserved.
// //
// This program is free software: you can redistribute it and/or modify // Permission is hereby granted, free of charge, to any person obtaining
// it under the terms of the GNU General Public License as published by // a copy of this software and associated documentation files (the
// the Free Software Foundation, either version 3 of the License, or // "Software"), to deal in the Software without restriction, including
// (at your option) any later version. // without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject
// to the following conditions:
// //
// This program is distributed in the hope that it will be useful, // The above copyright notice and this permission notice shall be
// but WITHOUT ANY WARRANTY; without even the implied warranty of // included in all copies or substantial portions of the Software.
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// //
// You should have received a copy of the GNU General Public License // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// along with this program. If not, see <http://www.gnu.org/licenses/>. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
// OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package flagger package flagger
// LoggerInterface provide logging interface, so everyone can inject own // LoggerInterface provide logging interface, so everyone can inject own
// logging handlers. // logging handlers.
type LoggerInterface interface { type LoggerInterface interface {
Fatalln(args ...interface{}) Fatalln(args ...interface{})
Println(v ...interface{}) Println(v ...interface{})
} }