Skip to content

Commit

Permalink
Merge pull request #444 from essentialkaos/develop
Browse files Browse the repository at this point in the history
Version 12.108.0
  • Loading branch information
andyone authored Mar 20, 2024
2 parents 808191d + dcf769e commit e427dfb
Show file tree
Hide file tree
Showing 30 changed files with 1,784 additions and 80 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ jobs:

- name: Run tests on Windows
working-directory: ${{env.SRC_DIR}}
run: .scripts/windows.sh 12
run: .scripts/windows.ps1

SendCoverage:
name: Send Coverage
Expand Down
1 change: 1 addition & 0 deletions .scripts/packages.list
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ L + netutil
* + spellcheck
* + spinner
* + strutil
* - support
* - system
* - system/container
* - system/exec
Expand Down
13 changes: 13 additions & 0 deletions .scripts/windows.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<# Download dependencies #>
Function Download-Deps {
go get -v golang.org/x/crypto/bcrypt
go get -v github.com/essentialkaos/depsy
}

<# Try to install everything #>
Function Check-Install {
go install ./...
}

Download-Deps
Check-Install
21 changes: 0 additions & 21 deletions .scripts/windows.sh

This file was deleted.

16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
## Changelog

### 12.108.0

* `[support]` Added new package for collecting support information
* `[options]` Added method `Map.Set`
* `[options]` Added method `Map.Delete`
* `[options]` Added shortcut `F` for method `Format`
* `[system/container]` Added method `IsContainer`
* `[system/container]` Added engine info caching
* `[pager]` Fixed panic when pager stdin is not a file
* `[usage]` Fixed bug with changing color for certain command or option
* `[lock]` Fixed build tags
* `[options]` Code refactoring
* `[usage]` Code refactoring
* `[options]` Tests refactoring
* `[system/container]` Added usage examples

### 12.107.0

* `[knf/united]` Added method `Simple`
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ go get -u github.com/essentialkaos/ek/v12
* [`spellcheck`](https://kaos.sh/g/ek.v12/spellcheck) — Package provides spellcheck based on Damerau–Levenshtein distance algorithm
* [`spinner`](https://kaos.sh/g/ek.v12/spinner) — Package provides methods for creating spinner animation for long-running tasks
* [`strutil`](https://kaos.sh/g/ek.v12/strutil) — Package provides methods for working with strings
* [`support`](https://kaos.sh/g/ek.v12/support) — Package provides methods for collecting and printing support information about system
* [`system/container`](https://kaos.sh/g/ek.v12/system/container) — Package provides methods for checking container engine info
* [`system/exec`](https://kaos.sh/g/ek.v12/system/exec) — Package provides methods for executing commands
* [`system/process`](https://kaos.sh/g/ek.v12/system/process) — Package provides methods for gathering information about active processes
Expand Down
2 changes: 1 addition & 1 deletion ek.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
// ////////////////////////////////////////////////////////////////////////////////// //

// VERSION is current ek package version
const VERSION = "12.107.0"
const VERSION = "12.108.0"

// ////////////////////////////////////////////////////////////////////////////////// //

Expand Down
3 changes: 3 additions & 0 deletions lock/lock.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//go:build !windows
// +build !windows

// Package lock provides methods for working with lock files
package lock

Expand Down
31 changes: 30 additions & 1 deletion options/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ func ExampleFormat() {

fmt.Printf("Option: %s\n", Format(o))
// Output:
// Option: --test/-t
// Option: -t/--test
}

// ////////////////////////////////////////////////////////////////////////////////// //
Expand Down Expand Up @@ -512,6 +512,35 @@ func ExampleArguments_Filter() {

// ////////////////////////////////////////////////////////////////////////////////// //

func ExampleMap_Set() {
m := Map{}

err := m.Set("t:test", &V{Value: "abcd"})

if err != nil {
panic(err.Error())
}
}

func ExampleMap_Delete() {
optName := "t:test"

m := Map{
optName: &V{Value: "abcd"},
}

if m.Delete(optName) {
fmt.Printf("Option %s deleted\n", F(optName))
} else {
fmt.Printf("There is no option %s\n", F(optName))
}

// Output:
// Option -t/--test deleted
}

// ////////////////////////////////////////////////////////////////////////////////// //

func ExampleArgument_String() {
opts := NewOptions()

Expand Down
72 changes: 58 additions & 14 deletions options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ const (
// Error codes
const (
ERROR_UNSUPPORTED = iota
ERROR_NO_NAME
ERROR_DUPLICATE_LONGNAME
ERROR_DUPLICATE_SHORTNAME
ERROR_OPTION_IS_NIL
Expand Down Expand Up @@ -85,8 +84,16 @@ type optionName struct {

// ////////////////////////////////////////////////////////////////////////////////// //

// ErrNilOptions returns if options struct is nil
var ErrNilOptions = fmt.Errorf("Options struct is nil")
var (
// ErrNilOptions returns if options struct is nil
ErrNilOptions = fmt.Errorf("Options struct is nil")

// ErrNilMap returns if options map is nil
ErrNilMap = fmt.Errorf("Options map is nil")

// ErrEmptyName returns if option have no name
ErrEmptyName = fmt.Errorf("One or more options do not have a name")
)

// ////////////////////////////////////////////////////////////////////////////////// //

Expand All @@ -113,10 +120,10 @@ func (opts *Options) Add(name string, option *V) error {
optName := parseName(name)

switch {
case optName.Long == "":
return ErrEmptyName
case option == nil:
return OptionError{"--" + optName.Long, "", ERROR_OPTION_IS_NIL}
case optName.Long == "":
return OptionError{"", "", ERROR_NO_NAME}
case opts.full[optName.Long] != nil:
return OptionError{"--" + optName.Long, "", ERROR_DUPLICATE_LONGNAME}
case optName.Short != "" && opts.short[optName.Short] != "":
Expand Down Expand Up @@ -146,6 +153,10 @@ func (opts *Options) Add(name string, option *V) error {

// AddMap adds map with supported options
func (opts *Options) AddMap(optMap Map) []error {
if optMap == nil {
return []error{ErrNilMap}
}

var errs []error

for name, opt := range optMap {
Expand Down Expand Up @@ -374,6 +385,38 @@ func (opts *Options) Parse(rawOpts []string, optMap ...Map) (Arguments, []error)

// ////////////////////////////////////////////////////////////////////////////////// //

// Set set option in map
// Note that if the option is already set, it will be replaced
func (m Map) Set(name string, opt *V) error {
optName := parseName(name)

switch {
case m == nil:
return ErrNilMap
case optName.Long == "":
return ErrEmptyName
case opt == nil:
return OptionError{"--" + optName.Long, "", ERROR_OPTION_IS_NIL}
}

m[name] = opt

return nil
}

// Delete removes option from map
func (m Map) Delete(name string) bool {
if m == nil || m[name] == nil {
return false
}

delete(m, name)

return true
}

// ////////////////////////////////////////////////////////////////////////////////// //

// NewOptions creates new options struct
func NewOptions() *Options {
return &Options{
Expand Down Expand Up @@ -489,7 +532,7 @@ func Format(opt string) string {
case a.Short == "":
return "--" + a.Long
default:
return fmt.Sprintf("--%s/-%s", a.Long, a.Short)
return fmt.Sprintf("-%s/--%s", a.Short, a.Long)
}
}

Expand All @@ -503,6 +546,11 @@ func Q(opts ...string) string {
return Merge(opts...)
}

// F formats option name (shortcut for Format)
func F(opt string) string {
return Format(opt)
}

// ////////////////////////////////////////////////////////////////////////////////// //

// I think it is okay to have such a long and complicated method for parsing data
Expand All @@ -517,12 +565,10 @@ func (opts *Options) parseOptions(rawOpts []string) (Arguments, []error) {
return nil, opts.validate()
}

var (
optName string
mixedOpt bool
arguments Arguments
errorList []error
)
var optName string
var mixedOpt bool
var arguments Arguments
var errorList []error

for _, curOpt := range rawOpts {
if optName == "" || mixedOpt {
Expand Down Expand Up @@ -903,8 +949,6 @@ func (e OptionError) Error() string {
return fmt.Sprintf("Struct for option %s is nil", e.Option)
case ERROR_DUPLICATE_LONGNAME, ERROR_DUPLICATE_SHORTNAME:
return fmt.Sprintf("Option %s defined 2 or more times", e.Option)
case ERROR_NO_NAME:
return "Some option does not have a name"
case ERROR_CONFLICT:
return fmt.Sprintf("Option %s conflicts with option %s", e.Option, e.BoundOption)
case ERROR_BOUND_NOT_SET:
Expand Down
Loading

0 comments on commit e427dfb

Please sign in to comment.