Skip to content

Commit

Permalink
Add go exit codes (#172)
Browse files Browse the repository at this point in the history
* Add ExitCode type

* Replace deprecated 'io/ioutil' usages with 'os' equivalents

* Update go modules

* Improve documentation

* Rename Fail to Failure
  • Loading branch information
hisaac authored Nov 30, 2022
1 parent 53c27f0 commit 5f2d645
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 30 deletions.
15 changes: 15 additions & 0 deletions exitcode/exitcode.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//go:generate stringer -type=ExitCode

package exitcode

// ExitCode is a simple integer type that represents an exit code.
// It can be used to provide a more semantically meaningful exit code than a simple integer.
type ExitCode int

const (
// Success indicates that the program exited successfully.
Success ExitCode = 0

// Failure indicates that the program exited unsuccessfully.
Failure ExitCode = 1
)
24 changes: 24 additions & 0 deletions exitcode/exitcode_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions fileutil/fileutil.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package fileutil

import (
"io/ioutil"
"os"
"path/filepath"
)
Expand Down Expand Up @@ -37,7 +36,7 @@ func (f fileManager) Write(path string, value string, mode os.FileMode) error {
if err := f.ensureSavePath(path); err != nil {
return err
}
if err := ioutil.WriteFile(path, []byte(value), mode); err != nil {
if err := os.WriteFile(path, []byte(value), mode); err != nil {
return err
}
return os.Chmod(path, mode)
Expand All @@ -50,5 +49,5 @@ func (fileManager) ensureSavePath(savePath string) error {

// WriteBytes ...
func (f fileManager) WriteBytes(path string, value []byte) error {
return ioutil.WriteFile(path, value, 0600)
return os.WriteFile(path, value, 0600)
}
8 changes: 4 additions & 4 deletions fileutil/fileutil_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package fileutil

import (
"io/ioutil"
"os"
"path/filepath"
"testing"

Expand All @@ -21,7 +21,7 @@ func TestWrite(t *testing.T) {
tmpFilePath := filepath.Join(tmpDirPath, "WriteStringToFile-success.txt")
require.NoError(t, manager.Write(tmpFilePath, content, 0600))

fileContent, err := ioutil.ReadFile(tmpFilePath)
fileContent, err := os.ReadFile(tmpFilePath)
require.NoError(t, err)
require.Equal(t, content, string(fileContent))
}
Expand All @@ -31,7 +31,7 @@ func TestWrite(t *testing.T) {
tmpFilePath := filepath.Join(tmpDirPath, "dir-does-not-exist", "WriteStringToFile-success.txt")
require.NoError(t, manager.Write(tmpFilePath, content, 0600))

fileContent, err := ioutil.ReadFile(tmpFilePath)
fileContent, err := os.ReadFile(tmpFilePath)
require.NoError(t, err)
require.Equal(t, content, string(fileContent))
}
Expand All @@ -41,7 +41,7 @@ func TestWrite(t *testing.T) {
tmpFilePath := filepath.Join(tmpDirPath, "WriteBytesToFile-success.txt")
require.NoError(t, manager.WriteBytes(tmpFilePath, []byte("test string")))

fileContent, err := ioutil.ReadFile(tmpFilePath)
fileContent, err := os.ReadFile(tmpFilePath)
require.NoError(t, err)
require.Equal(t, "test string", string(fileContent))
}
Expand Down
12 changes: 6 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ module github.com/bitrise-io/go-utils/v2
go 1.17

require (
github.com/gofrs/uuid v4.2.0+incompatible
github.com/gofrs/uuid v4.3.1+incompatible
github.com/hashicorp/go-retryablehttp v0.7.1
github.com/mitchellh/mapstructure v1.4.3
github.com/stretchr/testify v1.7.0
golang.org/x/sys v0.0.0-20220712014510-0a85c31ab51e
github.com/mitchellh/mapstructure v1.5.0
github.com/stretchr/testify v1.8.1
golang.org/x/sys v0.2.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/objx v0.3.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
github.com/stretchr/objx v0.5.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
28 changes: 15 additions & 13 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gofrs/uuid v4.2.0+incompatible h1:yyYWMnhkhrKwwr8gAOcOCYxOOscHgDS9yZgBrnJfGa0=
github.com/gofrs/uuid v4.2.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
github.com/gofrs/uuid v4.3.1+incompatible h1:0/KbAdpx3UXAx1kEOWHJeOkpbgRFGHVgv+CFIY7dBJI=
github.com/gofrs/uuid v4.3.1+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
github.com/hashicorp/go-cleanhttp v0.5.1/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ=
github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48=
github.com/hashicorp/go-hclog v0.9.2 h1:CG6TE5H9/JXsFWJCfoIVpKFIkFe6ysEuHirp4DxCsHI=
github.com/hashicorp/go-hclog v0.9.2/go.mod h1:5CU+agLiy3J7N7QjHK5d05KxGsuXiQLrjA0H7acj2lQ=
github.com/hashicorp/go-retryablehttp v0.7.1 h1:sUiuQAnLlbvmExtFQs72iFW/HXeUn8Z1aJLQ4LJJbTQ=
github.com/hashicorp/go-retryablehttp v0.7.1/go.mod h1:vAew36LZh98gCBJNLH42IQ1ER/9wtLZZ8meHqQvEYWY=
github.com/mitchellh/mapstructure v1.4.3 h1:OVowDSCllw/YjdLkam3/sm7wEtOy59d8ndGgCcyj8cs=
github.com/mitchellh/mapstructure v1.4.3/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.3.0 h1:NGXK3lHquSN08v5vWalVI/L8XU9hdzE/G6xsrze47As=
github.com/stretchr/objx v0.3.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/sys v0.0.0-20220712014510-0a85c31ab51e h1:NHvCuwuS43lGnYhten69ZWqi2QOj/CiDNcKbVqwVoew=
golang.org/x/sys v0.0.0-20220712014510-0a85c31ab51e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
golang.org/x/sys v0.2.0 h1:ljd4t30dBnAvMZaQCevtY0xLLD0A+bRZXbgLMLU1F/A=
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
3 changes: 1 addition & 2 deletions pathutil/pathutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package pathutil

import (
"errors"
"io/ioutil"
"os"
"os/user"
"path/filepath"
Expand All @@ -25,7 +24,7 @@ func NewPathProvider() PathProvider {
// If prefix is provided it'll be used as the tmp dir's name prefix.
// Normalized: it's guaranteed that the path won't end with '/'.
func (pathProvider) CreateTempDir(prefix string) (dir string, err error) {
dir, err = ioutil.TempDir("", prefix)
dir, err = os.MkdirTemp("", prefix)
dir = strings.TrimSuffix(dir, "/")

return
Expand Down
3 changes: 1 addition & 2 deletions pathutil/pathutil_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package pathutil

import (
"io/ioutil"
"os"
"os/user"
"path/filepath"
Expand Down Expand Up @@ -85,7 +84,7 @@ func Test_pathChecker_IsPathExists(t *testing.T) {
func Test_pathChecker_IsDirExists(t *testing.T) {
tmpDirPath := t.TempDir()
tmpFilePath := filepath.Join(t.TempDir(), "hello.txt")
require.NoError(t, ioutil.WriteFile(tmpFilePath, []byte("hello"), 0700))
require.NoError(t, os.WriteFile(tmpFilePath, []byte("hello"), 0700))

tests := []struct {
name string
Expand Down

0 comments on commit 5f2d645

Please sign in to comment.