-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add JSON utility functions, Viper configuration reader, IP utility fu…
…nctions, and file utility functions
- Loading branch information
Showing
12 changed files
with
198 additions
and
171 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package utils | ||
package conf | ||
|
||
import ( | ||
"github.com/spf13/viper" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package file | ||
|
||
import ( | ||
"crypto/md5" | ||
"encoding/hex" | ||
"io" | ||
"os" | ||
"path" | ||
"path/filepath" | ||
) | ||
|
||
func FilExists(filename string) bool { | ||
_, err := os.Stat(filename) | ||
return !os.IsNotExist(err) | ||
} | ||
|
||
func WriteToFile(filePath string, content string, filemode os.FileMode) error { | ||
if err := os.MkdirAll(filepath.Dir(filePath), os.ModePerm); err != nil { | ||
return err | ||
} | ||
|
||
if err := os.WriteFile(filePath, []byte(content), filemode); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func FileMD5(filePath string) (string, error) { | ||
file, err := os.Open(filePath) | ||
if err != nil { | ||
return "", err | ||
} | ||
defer file.Close() | ||
|
||
hash := md5.New() | ||
if _, err := io.Copy(hash, file); err != nil { | ||
return "", err | ||
} | ||
|
||
hashInBytes := hash.Sum(nil) | ||
md5Hash := hex.EncodeToString(hashInBytes) | ||
|
||
return md5Hash, nil | ||
} | ||
|
||
func MkdirAllIfNotExists(pathname string, perm os.FileMode) error { | ||
dir := path.Dir(pathname) | ||
if _, err := os.Stat(dir); err != nil { | ||
if os.IsNotExist(err) { | ||
if err := os.MkdirAll(dir, perm); err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package ip | ||
|
||
import ( | ||
"net" | ||
) | ||
|
||
func GetIPv4NetworkIPs() ([]string, error) { | ||
ips, err := GetNetworkIPs() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
ip4s := make([]string, 0) | ||
for _, ip := range ips { | ||
if net.ParseIP(ip).To4() != nil { | ||
ip4s = append(ip4s, ip) | ||
} | ||
} | ||
|
||
return ip4s, nil | ||
} | ||
|
||
func GetNetworkIPs() ([]string, error) { | ||
ifaces, err := net.Interfaces() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
ips := make([]string, 0) | ||
for _, i := range ifaces { | ||
addrs, err := i.Addrs() | ||
if err != nil { | ||
continue | ||
} | ||
for _, addr := range addrs { | ||
switch v := addr.(type) { | ||
case *net.IPNet: | ||
ips = append(ips, v.IP.String()) | ||
case *net.IPAddr: | ||
ips = append(ips, v.IP.String()) | ||
} | ||
} | ||
} | ||
|
||
return ips, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package josn | ||
|
||
import "encoding/json" | ||
|
||
func StructToJson(v interface{}) (string, error) { | ||
data, err := json.MarshalIndent(v, "", " ") | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return string(data), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package parse | ||
|
||
import "strconv" | ||
|
||
func ParseBool(val string, defVal bool) bool { | ||
_val, err := strconv.ParseBool(val) | ||
if err != nil { | ||
return defVal | ||
} | ||
return _val | ||
} | ||
|
||
func ParseInt(val string, defVal int) int { | ||
_val, err := strconv.Atoi(val) | ||
if err != nil { | ||
return defVal | ||
} | ||
return _val | ||
} | ||
|
||
func ParseInt64(val string, defVal int64) int64 { | ||
_val, err := strconv.ParseInt(val, 10, 64) | ||
if err != nil { | ||
return defVal | ||
} | ||
return _val | ||
} | ||
|
||
func ParseFloat64(val string, defVal float64) float64 { | ||
_val, err := strconv.ParseFloat(val, 64) | ||
if err != nil { | ||
return defVal | ||
} | ||
return _val | ||
} | ||
|
||
func ParseUint(val string, defVal uint) uint { | ||
_val, err := strconv.ParseUint(val, 10, 0) | ||
if err != nil { | ||
return defVal | ||
} | ||
return uint(_val) | ||
} |
Oops, something went wrong.