Skip to content

Commit 5f2d645

Browse files
authored
Add go exit codes (#172)
* Add ExitCode type * Replace deprecated 'io/ioutil' usages with 'os' equivalents * Update go modules * Improve documentation * Rename Fail to Failure
1 parent 53c27f0 commit 5f2d645

File tree

8 files changed

+68
-30
lines changed

8 files changed

+68
-30
lines changed

exitcode/exitcode.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//go:generate stringer -type=ExitCode
2+
3+
package exitcode
4+
5+
// ExitCode is a simple integer type that represents an exit code.
6+
// It can be used to provide a more semantically meaningful exit code than a simple integer.
7+
type ExitCode int
8+
9+
const (
10+
// Success indicates that the program exited successfully.
11+
Success ExitCode = 0
12+
13+
// Failure indicates that the program exited unsuccessfully.
14+
Failure ExitCode = 1
15+
)

exitcode/exitcode_string.go

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

fileutil/fileutil.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package fileutil
22

33
import (
4-
"io/ioutil"
54
"os"
65
"path/filepath"
76
)
@@ -37,7 +36,7 @@ func (f fileManager) Write(path string, value string, mode os.FileMode) error {
3736
if err := f.ensureSavePath(path); err != nil {
3837
return err
3938
}
40-
if err := ioutil.WriteFile(path, []byte(value), mode); err != nil {
39+
if err := os.WriteFile(path, []byte(value), mode); err != nil {
4140
return err
4241
}
4342
return os.Chmod(path, mode)
@@ -50,5 +49,5 @@ func (fileManager) ensureSavePath(savePath string) error {
5049

5150
// WriteBytes ...
5251
func (f fileManager) WriteBytes(path string, value []byte) error {
53-
return ioutil.WriteFile(path, value, 0600)
52+
return os.WriteFile(path, value, 0600)
5453
}

fileutil/fileutil_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package fileutil
22

33
import (
4-
"io/ioutil"
4+
"os"
55
"path/filepath"
66
"testing"
77

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

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

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

44-
fileContent, err := ioutil.ReadFile(tmpFilePath)
44+
fileContent, err := os.ReadFile(tmpFilePath)
4545
require.NoError(t, err)
4646
require.Equal(t, "test string", string(fileContent))
4747
}

go.mod

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@ module github.com/bitrise-io/go-utils/v2
33
go 1.17
44

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

1313
require (
1414
github.com/davecgh/go-spew v1.1.1 // indirect
1515
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
1616
github.com/pmezard/go-difflib v1.0.0 // indirect
17-
github.com/stretchr/objx v0.3.0 // indirect
18-
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
17+
github.com/stretchr/objx v0.5.0 // indirect
18+
gopkg.in/yaml.v3 v3.0.1 // indirect
1919
)

go.sum

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,32 @@
11
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
22
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
33
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4-
github.com/gofrs/uuid v4.2.0+incompatible h1:yyYWMnhkhrKwwr8gAOcOCYxOOscHgDS9yZgBrnJfGa0=
5-
github.com/gofrs/uuid v4.2.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
4+
github.com/gofrs/uuid v4.3.1+incompatible h1:0/KbAdpx3UXAx1kEOWHJeOkpbgRFGHVgv+CFIY7dBJI=
5+
github.com/gofrs/uuid v4.3.1+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
66
github.com/hashicorp/go-cleanhttp v0.5.1/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
77
github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ=
88
github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48=
99
github.com/hashicorp/go-hclog v0.9.2 h1:CG6TE5H9/JXsFWJCfoIVpKFIkFe6ysEuHirp4DxCsHI=
1010
github.com/hashicorp/go-hclog v0.9.2/go.mod h1:5CU+agLiy3J7N7QjHK5d05KxGsuXiQLrjA0H7acj2lQ=
1111
github.com/hashicorp/go-retryablehttp v0.7.1 h1:sUiuQAnLlbvmExtFQs72iFW/HXeUn8Z1aJLQ4LJJbTQ=
1212
github.com/hashicorp/go-retryablehttp v0.7.1/go.mod h1:vAew36LZh98gCBJNLH42IQ1ER/9wtLZZ8meHqQvEYWY=
13-
github.com/mitchellh/mapstructure v1.4.3 h1:OVowDSCllw/YjdLkam3/sm7wEtOy59d8ndGgCcyj8cs=
14-
github.com/mitchellh/mapstructure v1.4.3/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
13+
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
14+
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
1515
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
1616
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
1717
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
18-
github.com/stretchr/objx v0.3.0 h1:NGXK3lHquSN08v5vWalVI/L8XU9hdzE/G6xsrze47As=
19-
github.com/stretchr/objx v0.3.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
18+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
19+
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
20+
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
2021
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
21-
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
22-
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
23-
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
24-
golang.org/x/sys v0.0.0-20220712014510-0a85c31ab51e h1:NHvCuwuS43lGnYhten69ZWqi2QOj/CiDNcKbVqwVoew=
25-
golang.org/x/sys v0.0.0-20220712014510-0a85c31ab51e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
22+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
23+
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
24+
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
25+
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
26+
golang.org/x/sys v0.2.0 h1:ljd4t30dBnAvMZaQCevtY0xLLD0A+bRZXbgLMLU1F/A=
27+
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
2628
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
2729
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
2830
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
29-
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
30-
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
31+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
32+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

pathutil/pathutil.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package pathutil
22

33
import (
44
"errors"
5-
"io/ioutil"
65
"os"
76
"os/user"
87
"path/filepath"
@@ -25,7 +24,7 @@ func NewPathProvider() PathProvider {
2524
// If prefix is provided it'll be used as the tmp dir's name prefix.
2625
// Normalized: it's guaranteed that the path won't end with '/'.
2726
func (pathProvider) CreateTempDir(prefix string) (dir string, err error) {
28-
dir, err = ioutil.TempDir("", prefix)
27+
dir, err = os.MkdirTemp("", prefix)
2928
dir = strings.TrimSuffix(dir, "/")
3029

3130
return

pathutil/pathutil_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package pathutil
22

33
import (
4-
"io/ioutil"
54
"os"
65
"os/user"
76
"path/filepath"
@@ -85,7 +84,7 @@ func Test_pathChecker_IsPathExists(t *testing.T) {
8584
func Test_pathChecker_IsDirExists(t *testing.T) {
8685
tmpDirPath := t.TempDir()
8786
tmpFilePath := filepath.Join(t.TempDir(), "hello.txt")
88-
require.NoError(t, ioutil.WriteFile(tmpFilePath, []byte("hello"), 0700))
87+
require.NoError(t, os.WriteFile(tmpFilePath, []byte("hello"), 0700))
8988

9089
tests := []struct {
9190
name string

0 commit comments

Comments
 (0)