Skip to content

Commit

Permalink
Merge pull request #2 from essentialkaos/develop
Browse files Browse the repository at this point in the history
Release 4
  • Loading branch information
andyone committed Nov 25, 2015
2 parents 3acaed7 + bdbd4c1 commit 3f9e008
Show file tree
Hide file tree
Showing 6 changed files with 277 additions and 15 deletions.
6 changes: 6 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
### Changelog

#### r4

* [system] Added json tags for User, Group and SessionInfo structs
* [usage] Info now can use os.Args[0] for info rendering
* [version] Added package for working with version in simver notation

#### r3

* [arg] Changed default fail values (int -1 → 0, float -1.0 → 0.0)
Expand Down
9 changes: 9 additions & 0 deletions ek.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Auxiliary packages for Go
package ek

// ////////////////////////////////////////////////////////////////////////////////// //
// //
// Copyright (c) 2009-2015 Essential Kaos //
// Essential Kaos Open Source License <http://essentialkaos.com/ekol?en> //
// //
// ////////////////////////////////////////////////////////////////////////////////// //
30 changes: 15 additions & 15 deletions system/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,29 @@ const _PTS_DIR = "/dev/pts"

// User contains information about user
type User struct {
UID int
GID int
Name string
Groups []*Group
Comment string
Shell string
HomeDir string
RealUID int
RealGID int
RealName string
UID int `json:"uid"`
GID int `json:"gid"`
Name string `json:"name"`
Groups []*Group `json:"groups"`
Comment string `json:"comment"`
Shell string `json:"shell"`
HomeDir string `json:"home_dir"`
RealUID int `json:"real_uid"`
RealGID int `json:"real_gid"`
RealName string `json:"real_name"`
}

// Group contains information about group
type Group struct {
Name string
GID int
Name string `json:"name"`
GID int `json:"gid"`
}

// SessionInfo contains information about all sessions
type SessionInfo struct {
Name string
LoginTime time.Time
LastActivityTime time.Time
Name string `json:"name"`
LoginTime time.Time `json:"login_time"`
LastActivityTime time.Time `json:"last_activity_time"`
}

type sessionsInfo []*SessionInfo
Expand Down
6 changes: 6 additions & 0 deletions usage/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ package usage

import (
"fmt"
"os"
"path/filepath"
"strings"
"time"

Expand Down Expand Up @@ -61,6 +63,10 @@ type example struct {

// NewInfo create new info struct
func NewInfo(name string, args ...string) *Info {
if name == "" {
return &Info{name: filepath.Base(os.Args[0]), args: strings.Join(args, " ")}
}

return &Info{name: name, args: strings.Join(args, " ")}
}

Expand Down
131 changes: 131 additions & 0 deletions version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
// Package provides methods for parsing semver version info
package version

// ////////////////////////////////////////////////////////////////////////////////// //
// //
// Copyright (c) 2009-2015 Essential Kaos //
// Essential Kaos Open Source License <http://essentialkaos.com/ekol?en> //
// //
// ////////////////////////////////////////////////////////////////////////////////// //

import (
"strconv"
"strings"
)

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

type Version struct {
raw string
versionSlice []int
preRelease string
build string
}

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

func Parse(ver string) *Version {
if ver == "" {
return &Version{}
}

result := &Version{raw: ver}

if strings.Contains(ver, "+") {
bs := strings.Split(ver, "+")

if bs[1] == "" {
ver = bs[0]
} else {
ver = bs[0]
result.build = bs[1]
}
}

if strings.Contains(ver, "-") {
ps := strings.Split(ver, "-")

if ps[1] == "" {
ver = ps[0]
} else {
ver = ps[0]
result.preRelease = ps[1]
}
}

for _, version := range strings.Split(ver, ".") {
iv, err := strconv.Atoi(version)

if err != nil {
break
}

result.versionSlice = append(result.versionSlice, iv)
}

return result
}

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

// Major return major version
func (v *Version) Major() int {
if v == nil || v.raw == "" || len(v.versionSlice) == 0 {
return -1
}

return v.versionSlice[0]
}

// Minor return minor version
func (v *Version) Minor() int {
if v == nil || v.raw == "" || len(v.versionSlice) == 0 {
return -1
}

if len(v.versionSlice) == 1 {
return 0
}

return v.versionSlice[1]
}

// Patch return patch version
func (v *Version) Patch() int {
if v == nil || v.raw == "" || len(v.versionSlice) == 0 {
return -1
}

if len(v.versionSlice) <= 2 {
return 0
}

return v.versionSlice[2]
}

// PreRelease return prerelease version
func (v *Version) PreRelease() string {
if v == nil || v.raw == "" {
return ""
}

return v.preRelease
}

// Build return build
func (v *Version) Build() string {
if v == nil || v.raw == "" {
return ""
}

return v.build
}

// String return version as string
func (v *Version) String() string {
if v == nil {
return ""
}

return v.raw
}
110 changes: 110 additions & 0 deletions version/version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package version

// ////////////////////////////////////////////////////////////////////////////////// //
// //
// Copyright (c) 2009-2015 Essential Kaos //
// Essential Kaos Open Source License <http://essentialkaos.com/ekol?en> //
// //
// ////////////////////////////////////////////////////////////////////////////////// //

import (
"testing"

. "gopkg.in/check.v1"
)

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

func Test(t *testing.T) { TestingT(t) }

type VersionSuite struct{}

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

var _ = Suite(&VersionSuite{})

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

func (s *VersionSuite) TestMajor(c *C) {
c.Assert(Parse("1").Major(), Equals, 1)
c.Assert(Parse("2.0.0").Major(), Equals, 2)
c.Assert(Parse("3.4.5").Major(), Equals, 3)
c.Assert(Parse("4-beta1").Major(), Equals, 4)
c.Assert(Parse("5-beta2+exp.sha.5114f85").Major(), Equals, 5)
}

func (s *VersionSuite) TestMinor(c *C) {
c.Assert(Parse("1").Minor(), Equals, 0)
c.Assert(Parse("2.1").Minor(), Equals, 1)
c.Assert(Parse("3.4.5").Minor(), Equals, 4)
c.Assert(Parse("4-alpha1").Minor(), Equals, 0)
c.Assert(Parse("5-beta1+sha:5114f85").Minor(), Equals, 0)
c.Assert(Parse("6.12.1-beta2+exp.sha.5114f85").Minor(), Equals, 12)
}

func (s *VersionSuite) TestPatch(c *C) {
c.Assert(Parse("1").Patch(), Equals, 0)
c.Assert(Parse("2.1").Patch(), Equals, 0)
c.Assert(Parse("3.4.5").Patch(), Equals, 5)
c.Assert(Parse("4-alpha1").Patch(), Equals, 0)
c.Assert(Parse("5-beta1+sha:5114f85").Patch(), Equals, 0)
c.Assert(Parse("6.12.1-beta2+exp.sha.5114f85").Patch(), Equals, 1)
}

func (s *VersionSuite) TestPre(c *C) {
c.Assert(Parse("1").PreRelease(), Equals, "")
c.Assert(Parse("2.1").PreRelease(), Equals, "")
c.Assert(Parse("3.4.5").PreRelease(), Equals, "")
c.Assert(Parse("3.4.5-").PreRelease(), Equals, "")
c.Assert(Parse("4-alpha1").PreRelease(), Equals, "alpha1")
c.Assert(Parse("5-beta1+sha:5114f85").PreRelease(), Equals, "beta1")
c.Assert(Parse("6.12.1-beta2+exp.sha.5114f85").PreRelease(), Equals, "beta2")
}

func (s *VersionSuite) TestBuild(c *C) {
c.Assert(Parse("1").Build(), Equals, "")
c.Assert(Parse("2.1").Build(), Equals, "")
c.Assert(Parse("3.4.5").Build(), Equals, "")
c.Assert(Parse("4-alpha1+").Build(), Equals, "")
c.Assert(Parse("5-beta1+sha:5114f85").Build(), Equals, "sha:5114f85")
c.Assert(Parse("6.12.1-beta2+exp.sha.5114f85").Build(), Equals, "exp.sha.5114f85")
}

func (s *VersionSuite) TestString(c *C) {
c.Assert(Parse("1").String(), Equals, "1")
c.Assert(Parse("2.1").String(), Equals, "2.1")
c.Assert(Parse("3.4.5").String(), Equals, "3.4.5")
c.Assert(Parse("5-beta1+sha:5114f85").String(), Equals, "5-beta1+sha:5114f85")
c.Assert(Parse("6.12.1-beta2+exp.sha.5114f85").String(), Equals, "6.12.1-beta2+exp.sha.5114f85")
}

func (s *VersionSuite) TestErrors(c *C) {
var v1 *Version
var v2 = &Version{}

c.Assert(Parse("A").Major(), Equals, -1)
c.Assert(Parse("A").Minor(), Equals, -1)
c.Assert(Parse("A").Patch(), Equals, -1)
c.Assert(Parse("A").PreRelease(), Equals, "")
c.Assert(Parse("A").Build(), Equals, "")

c.Assert(Parse("").Major(), Equals, -1)
c.Assert(Parse("").Minor(), Equals, -1)
c.Assert(Parse("").Patch(), Equals, -1)
c.Assert(Parse("").PreRelease(), Equals, "")
c.Assert(Parse("").Build(), Equals, "")

c.Assert(v1.Major(), Equals, -1)
c.Assert(v1.Minor(), Equals, -1)
c.Assert(v1.Patch(), Equals, -1)
c.Assert(v1.PreRelease(), Equals, "")
c.Assert(v1.Build(), Equals, "")
c.Assert(v1.String(), Equals, "")

c.Assert(v2.Major(), Equals, -1)
c.Assert(v2.Minor(), Equals, -1)
c.Assert(v2.Patch(), Equals, -1)
c.Assert(v2.PreRelease(), Equals, "")
c.Assert(v2.Build(), Equals, "")
c.Assert(v2.String(), Equals, "")
}

0 comments on commit 3f9e008

Please sign in to comment.