Skip to content

Commit

Permalink
chore: refactor (#28)
Browse files Browse the repository at this point in the history
* chore: moved Module to constants

* chore: moved clients to separate folder

* chore: use custom http client for tendermint

* chore: return QueryInfo on query in Git client

* chore: remove colors from Cosmovisor output in logging

* chore: return QueryInfo on query in Cosmovisor version fetching

* chore: return QueryInfo on query in all clients

* chore: moved actions to constants

* chore: fixed linting
  • Loading branch information
freak12techno authored Mar 23, 2024
1 parent 7bf3920 commit 5069c3c
Show file tree
Hide file tree
Showing 16 changed files with 237 additions and 153 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import (
"errors"
"fmt"
"main/pkg/config"
"main/pkg/constants"
"main/pkg/query_info"
"main/pkg/types"
"main/pkg/utils"
"os"
"os/exec"
"strings"
Expand Down Expand Up @@ -43,43 +46,65 @@ func getJsonString(input string) string {
return input
}

func (c *Cosmovisor) GetVersion() (types.VersionInfo, error) {
func (c *Cosmovisor) GetVersion() (types.VersionInfo, query_info.QueryInfo, error) {
queryInfo := query_info.QueryInfo{
Module: constants.ModuleCosmovisor,
Action: constants.ActionCosmovisorGetVersion,
Success: false,
}

cmd := exec.Command(c.Config.CosmovisorPath, "run", "version", "--long", "--output", "json")
cmd.Env = append(
os.Environ(),
fmt.Sprintf("DAEMON_NAME=%s", c.Config.ChainBinaryName),
fmt.Sprintf("DAEMON_HOME=%s", c.Config.ChainFolder),
"DAEMON_NAME="+c.Config.ChainBinaryName,
"DAEMON_HOME="+c.Config.ChainFolder,
)

out, err := cmd.CombinedOutput()
if err != nil {
c.Logger.Error().Err(err).Str("output", string(out)).Msg("Could not get app version")
return types.VersionInfo{}, err
c.Logger.Error().
Err(err).
Str("output", utils.DecolorifyString(string(out))).
Msg("Could not get app version")
return types.VersionInfo{}, queryInfo, err
}

jsonOutput := getJsonString(string(out))

var versionInfo types.VersionInfo
if err := json.Unmarshal([]byte(jsonOutput), &versionInfo); err != nil {
c.Logger.Error().Err(err).Str("output", jsonOutput).Msg("Could not unmarshall app version")
return versionInfo, err
c.Logger.Error().
Err(err).
Str("output", jsonOutput).
Msg("Could not unmarshall app version")
return versionInfo, queryInfo, err
}

return versionInfo, nil
queryInfo.Success = true
return versionInfo, queryInfo, nil
}

func (c *Cosmovisor) GetCosmovisorVersion() (string, error) {
func (c *Cosmovisor) GetCosmovisorVersion() (string, query_info.QueryInfo, error) {
queryInfo := query_info.QueryInfo{
Module: constants.ModuleCosmovisor,
Action: constants.ActionCosmovisorGetCosmovisorVersion,
Success: false,
}

cmd := exec.Command(c.Config.CosmovisorPath, "version")
cmd.Env = append(
os.Environ(),
fmt.Sprintf("DAEMON_NAME=%s", c.Config.ChainBinaryName),
fmt.Sprintf("DAEMON_HOME=%s", c.Config.ChainFolder),
"DAEMON_NAME="+c.Config.ChainBinaryName,
"DAEMON_HOME="+c.Config.ChainFolder,
)

out, err := cmd.CombinedOutput()
if err != nil {
c.Logger.Error().Err(err).Str("output", string(out)).Msg("Could not get Cosmovisor version")
return "", err
c.Logger.Error().
Err(err).
Str("output", utils.DecolorifyString(string(out))).
Msg("Could not get Cosmovisor version")
return "", queryInfo, err
}

outSplit := strings.Split(string(out), "\n")
Expand All @@ -88,19 +113,26 @@ func (c *Cosmovisor) GetCosmovisorVersion() (string, error) {

for _, outString := range outSplit {
if strings.HasPrefix(outString, cosmovisorVersionPrefix) {
return outString[len(cosmovisorVersionPrefix):], nil
queryInfo.Success = true
return outString[len(cosmovisorVersionPrefix):], queryInfo, nil
}
}

return "", fmt.Errorf("could not find version in Cosmovisor response")
return "", queryInfo, errors.New("could not find version in Cosmovisor response")
}

func (c *Cosmovisor) GetUpgrades() (types.UpgradesPresent, error) {
upgradesFolder := fmt.Sprintf("%s/cosmovisor/upgrades", c.Config.ChainFolder)
func (c *Cosmovisor) GetUpgrades() (types.UpgradesPresent, query_info.QueryInfo, error) {
cosmovisorGetUpgradesQueryInfo := query_info.QueryInfo{
Action: constants.ActionCosmovisorGetUpgrades,
Module: constants.ModuleCosmovisor,
Success: false,
}

upgradesFolder := c.Config.ChainFolder + "/cosmovisor/upgrades"
upgradesFolderContent, err := os.ReadDir(upgradesFolder)
if err != nil {
c.Logger.Error().Err(err).Msg("Could not fetch Cosmovisor upgrades folder content")
return map[string]bool{}, err
return map[string]bool{}, cosmovisorGetUpgradesQueryInfo, err
}

upgrades := map[string]bool{}
Expand All @@ -127,5 +159,7 @@ func (c *Cosmovisor) GetUpgrades() (types.UpgradesPresent, error) {
}
}

return upgrades, nil
cosmovisorGetUpgradesQueryInfo.Success = true

return upgrades, cosmovisorGetUpgradesQueryInfo, nil
}
3 changes: 2 additions & 1 deletion pkg/git/client.go → pkg/clients/git/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package git
import (
configPkg "main/pkg/config"
"main/pkg/constants"
"main/pkg/query_info"

"github.com/rs/zerolog"
)

type Client interface {
GetLatestRelease() (string, error)
GetLatestRelease() (string, query_info.QueryInfo, error)
}

func GetClient(config configPkg.NodeConfig, logger zerolog.Logger) Client {
Expand Down
24 changes: 17 additions & 7 deletions pkg/git/github.go → pkg/clients/git/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"main/pkg/config"
"main/pkg/constants"
"main/pkg/query_info"
"net/http"
"time"

Expand Down Expand Up @@ -51,20 +52,26 @@ func (g *Github) UseCache() bool {
return diff < constants.UncachedGithubQueryTime
}

func (g *Github) GetLatestRelease() (string, error) {
func (g *Github) GetLatestRelease() (string, query_info.QueryInfo, error) {
latestReleaseUrl := fmt.Sprintf(
"https://api.github.com/repos/%s/%s/releases/latest",
g.Organization,
g.Repository,
)

queryInfo := query_info.QueryInfo{
Module: constants.ModuleGit,
Action: constants.ActionGitGetLatestRelease,
Success: false,
}

client := &http.Client{
Timeout: 10 * time.Second,
}

req, err := http.NewRequest(http.MethodGet, latestReleaseUrl, nil)
if err != nil {
return "", err
return "", queryInfo, err
}

useCache := g.UseCache()
Expand All @@ -86,29 +93,32 @@ func (g *Github) GetLatestRelease() (string, error) {

res, err := client.Do(req)
if err != nil {
return "", err
return "", queryInfo, err
}
defer res.Body.Close()

if res.StatusCode == http.StatusNotModified && g.LastResult != "" {
queryInfo.Success = true
g.Logger.Trace().Msg("Github returned cached response")
return g.LastResult, nil
return g.LastResult, queryInfo, nil
}

releaseInfo := GithubReleaseInfo{}
err = json.NewDecoder(res.Body).Decode(&releaseInfo)

if err != nil {
return "", err
return "", queryInfo, err
}

// GitHub returned error, probably rate-limiting
if releaseInfo.Message != "" {
return "", fmt.Errorf("got error from Github: %s", releaseInfo.Message)
return "", queryInfo, fmt.Errorf("got error from Github: %s", releaseInfo.Message)
}

g.LastModified = time.Now()
g.LastResult = releaseInfo.TagName

return releaseInfo.TagName, err
queryInfo.Success = true

return releaseInfo.TagName, queryInfo, err
}
24 changes: 17 additions & 7 deletions pkg/git/gitopia.go → pkg/clients/git/gitopia.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package git

import (
"encoding/json"
"errors"
"fmt"
"main/pkg/config"
"main/pkg/constants"
"main/pkg/query_info"
"net/http"
"time"

Expand Down Expand Up @@ -36,20 +38,26 @@ func NewGitopia(config config.NodeConfig, logger zerolog.Logger) *Gitopia {
}
}

func (g *Gitopia) GetLatestRelease() (string, error) {
func (g *Gitopia) GetLatestRelease() (string, query_info.QueryInfo, error) {
latestReleaseUrl := fmt.Sprintf(
"https://api.gitopia.com/gitopia/gitopia/gitopia/%s/repository/%s/releases/latest",
g.Organization,
g.Repository,
)

queryInfo := query_info.QueryInfo{
Module: constants.ModuleGit,
Action: constants.ActionGitGetLatestRelease,
Success: false,
}

client := &http.Client{
Timeout: 10 * time.Second,
}

req, err := http.NewRequest(http.MethodGet, latestReleaseUrl, nil)
if err != nil {
return "", err
return "", queryInfo, err
}

g.Logger.Trace().
Expand All @@ -58,24 +66,26 @@ func (g *Gitopia) GetLatestRelease() (string, error) {

res, err := client.Do(req)
if err != nil {
return "", err
return "", queryInfo, err
}
defer res.Body.Close()

response := GitopiaResponse{}
err = json.NewDecoder(res.Body).Decode(&response)

if err != nil {
return "", err
return "", queryInfo, err
}

if response.Message != "" {
return "", fmt.Errorf(response.Message)
return "", queryInfo, errors.New(response.Message)
}

if response.Release == nil {
return "", fmt.Errorf("malformed response from Gitopia")
return "", queryInfo, errors.New("malformed response from Gitopia")
}

return response.Release.TagName, err
queryInfo.Success = true

return response.Release.TagName, queryInfo, err
}
Loading

0 comments on commit 5069c3c

Please sign in to comment.