Skip to content

Commit

Permalink
Add documents for godoc (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
mpppk committed Jul 28, 2019
1 parent e5bab45 commit 9b65443
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 12 deletions.
6 changes: 6 additions & 0 deletions internal/option/config.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package option provides utilities of option handling
package option

import (
Expand All @@ -6,18 +7,21 @@ import (
"github.com/spf13/viper"
)

// Flag represents flag which can be specified
type Flag struct {
IsPersistent bool
Shorthand string
Name string
Usage string
}

// StringFlag represents flag which can be specified as string
type StringFlag struct {
*Flag
Value string
}

// BoolFlag represents flag which can be specified as bool
type BoolFlag struct {
*Flag
Value bool
Expand All @@ -31,6 +35,7 @@ func getFlagSet(cmd *cobra.Command, flagConfig *Flag) (flagSet *pflag.FlagSet) {
}
}

// RegisterStringFlag register string flag to provided cmd and viper
func RegisterStringFlag(cmd *cobra.Command, flagConfig *StringFlag) error {
flagSet := getFlagSet(cmd, flagConfig.Flag)
if flagConfig.Shorthand == "" {
Expand All @@ -45,6 +50,7 @@ func RegisterStringFlag(cmd *cobra.Command, flagConfig *StringFlag) error {
return nil
}

// RegisterBoolFlag register bool flag to provided cmd and viper
func RegisterBoolFlag(cmd *cobra.Command, flagConfig *BoolFlag) error {
flagSet := getFlagSet(cmd, flagConfig.Flag)
if flagConfig.Shorthand == "" {
Expand Down
2 changes: 2 additions & 0 deletions internal/selfupdate/selfupdate.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package selfupdate provides function to update binary
package selfupdate

import (
Expand All @@ -9,6 +10,7 @@ import (
const Version = "1.2.3"
const slug = "mpppk/cli-template"

// Do execute updating binary
func Do() (bool, error) {
v := semver.MustParse(Version)
latest, err := selfupdate.UpdateSelf(v, slug)
Expand Down
4 changes: 4 additions & 0 deletions pkg/sum/sum.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
// Package sum provides utilities for calculate sum of numbers
package sum

import (
"github.com/mpppk/cli-template/pkg/util"
"math"
)

// Sum returns sum of numbers
func Sum(numbers []int) (sum int) {
for _, number := range numbers {
sum += number
}
return
}

// L1Norm returns L1 norm of numbers
func L1Norm(numbers []int) (l1norm int) {
var absNumbers []int
for _, number := range numbers {
Expand All @@ -20,6 +23,7 @@ func L1Norm(numbers []int) (l1norm int) {
return Sum(absNumbers)
}

// SumFromString returns sum numbers which be converted from strings
func SumFromString(stringNumbers []string) (sum int, err error) {
numbers, err := util.ConvertStringSliceToIntSlice(stringNumbers)
if err != nil {
Expand Down
14 changes: 2 additions & 12 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// Package util provides some utilities
package util

import (
"fmt"
"os"
"strconv"

"golang.org/x/xerrors"
)

// ConvertStringSliceToIntSlice converts string slices to int slices.
func ConvertStringSliceToIntSlice(stringSlice []string) (intSlice []int, err error) {
for _, s := range stringSlice {
num, err := strconv.Atoi(s)
Expand All @@ -18,13 +18,3 @@ func ConvertStringSliceToIntSlice(stringSlice []string) (intSlice []int, err err
}
return
}

func EPrintlnIFErrExist(err error, msg string) (bool, error) {
if err != nil {
if _, err := fmt.Fprintln(os.Stderr, msg); err != nil {
return true, err
}
return true, nil
}
return false, nil
}

0 comments on commit 9b65443

Please sign in to comment.