Run gofumpt on the codebase (#58)

This commit is contained in:
Dave G
2021-12-19 00:55:33 -05:00
committed by GitHub
parent 389400a974
commit 666cbee43a
6 changed files with 9 additions and 30 deletions

View File

@@ -2,13 +2,12 @@ package cmd
import (
"encoding/base64"
"time"
"fmt"
"io/ioutil"
"math/rand"
"os"
"strings"
"time"
"github.com/AlecAivazis/survey/v2"
"github.com/rs/zerolog/log"
@@ -17,8 +16,10 @@ import (
"github.com/spf13/cobra"
)
var flagRandom bool
var flagReconnect bool
var (
flagRandom bool
flagReconnect bool
)
func init() {
connectCmd.Flags().BoolVarP(&flagRandom, "random", "r", false, "connect to a random server")
@@ -32,9 +33,7 @@ var connectCmd = &cobra.Command{
Long: `Connect to a vpn from a list of relay servers`,
Args: cobra.RangeArgs(0, 1),
Run: func(cmd *cobra.Command, args []string) {
vpnServers, err := vpn.GetList()
if err != nil {
log.Fatal().Msgf(err.Error())
os.Exit(1)
@@ -120,6 +119,5 @@ var connectCmd = &cobra.Command{
}
}
},
}

View File

@@ -20,9 +20,7 @@ var listCmd = &cobra.Command{
Short: "List all available vpn servers",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
vpnServers, err := vpn.GetList()
if err != nil {
log.Fatal().Msgf(err.Error())
os.Exit(1)
@@ -35,6 +33,5 @@ var listCmd = &cobra.Command{
table.Append([]string{strconv.Itoa(i + 1), v.HostName, v.CountryLong, v.Ping, strconv.Itoa(v.Score)})
}
table.Render() // Send output
},
}

View File

@@ -25,20 +25,18 @@ func getCacheDir() string {
func createCacheDir() error {
cacheDir := getCacheDir()
var AppFs = afero.NewOsFs()
return AppFs.MkdirAll(cacheDir, 0700)
AppFs := afero.NewOsFs()
return AppFs.MkdirAll(cacheDir, 0o700)
}
func getVpnListCache() (*[]Server, error) {
cacheFile := path.Join(getCacheDir(), serverCachefile)
serversFile, err := os.Open(cacheFile)
if err != nil {
return nil, err
}
byteValue, err := ioutil.ReadAll(serversFile)
if err != nil {
return nil, err
}
@@ -55,30 +53,25 @@ func getVpnListCache() (*[]Server, error) {
}
func writeVpnListToCache(servers []Server) error {
err := createCacheDir()
if err != nil {
return err
}
f, err := json.MarshalIndent(servers, "", " ")
if err != nil {
return err
}
cacheFile := path.Join(getCacheDir(), serverCachefile)
err = ioutil.WriteFile(cacheFile, f, 0644)
err = ioutil.WriteFile(cacheFile, f, 0o644)
return err
}
func vpnListCacheIsExpired() bool {
file, err := os.Stat(path.Join(getCacheDir(), serverCachefile))
if err != nil {
return true
}

View File

@@ -12,7 +12,6 @@ import (
// Connect to a specified OpenVPN configuration
func Connect(configPath string) error {
tmpLogFile, err := ioutil.TempFile("", "vpngate-openvpn-log-")
if err != nil {
return errors.Annotate(err, "Unable to create a temporary log file")

View File

@@ -1,10 +1,9 @@
package vpn
import (
"net/http"
"bytes"
"io"
"net/http"
"github.com/jszwec/csvutil"
"github.com/rs/zerolog/log"
@@ -38,7 +37,6 @@ func streamToBytes(stream io.Reader) []byte {
// parse csv
func parseVpnList(r io.Reader) (*[]Server, error) {
var servers []Server
serverList := streamToBytes(r)
@@ -52,12 +50,10 @@ func parseVpnList(r io.Reader) (*[]Server, error) {
}
return &servers, nil
}
// GetList returns a list of vpn servers
func GetList() (*[]Server, error) {
cacheExpired := vpnListCacheIsExpired()
var servers *[]Server
@@ -78,7 +74,6 @@ func GetList() (*[]Server, error) {
log.Info().Msg("Fetching the latest server list")
r, err := http.Get(vpnList)
if err != nil {
return nil, errors.Annotate(err, "Unable to retrieve vpn list")
}

View File

@@ -12,12 +12,10 @@ func TestGetListReal(t *testing.T) {
_, err := GetList()
assert.NoError(t, err)
}
// TestParseVpnList parses a local copy of vpn list csv
func TestParseVpnList(t *testing.T) {
dat, err := os.Open("../../test_data/vpn_list.csv")
assert.NoError(t, err)
@@ -31,5 +29,4 @@ func TestParseVpnList(t *testing.T) {
assert.Equal(t, (*servers)[0].HostName, "public-vpn-227")
assert.Equal(t, (*servers)[0].Ping, "13")
assert.Equal(t, (*servers)[0].Score, 2086924)
}