Skip to content

Commit

Permalink
Merge pull request #1146 from mrpalide/fix/change-to-exported-parse-o…
Browse files Browse the repository at this point in the history
…ptions-fields

make exported ParseOptions fileds
  • Loading branch information
ersonp authored Apr 7, 2022
2 parents 21cd90d + 755a3ed commit c50ebe4
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 163 deletions.
4 changes: 2 additions & 2 deletions cmd/skywire-visor/commands/nosystray.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ func extraFlags() {

}

func runApp(args []string) {
runVisor(args)
func runApp() {
runVisor()
}

// setStopFunction sets the stop function
Expand Down
116 changes: 55 additions & 61 deletions cmd/skywire-visor/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"os"
"os/exec"
"os/user"
"regexp"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -202,7 +201,7 @@ var rootCmd = &cobra.Command{
//retrieve build info
skyenv.BuildInfo = buildinfo.Get()
if skyenv.BuildInfo.Version == "unknown" {
if match, err := regexp.MatchString("/tmp/", skyenv.Skywire); err == nil {
if match := strings.Contains("/tmp/", skyenv.Skywire); err == nil {
if match {
log.Info("executed with go run")
log.WithField("binary: ", skyenv.Skywire).Info()
Expand All @@ -227,7 +226,7 @@ var rootCmd = &cobra.Command{
fork = strings.ReplaceAll(fork, "github.com/", "")
fork = strings.ReplaceAll(fork, ":/", "")
fork = strings.ReplaceAll(fork, "\n", "")
if nofork, err := regexp.MatchString(fork, "skycoin/skywire"); err == nil {
if nofork := strings.Contains(fork, "skycoin/skywire"); err == nil {
if !nofork {
fork = ""
}
Expand Down Expand Up @@ -261,13 +260,13 @@ var rootCmd = &cobra.Command{
log.WithField("branch: ", branch).Info()
}
},
Run: func(_ *cobra.Command, args []string) {
runApp(args)
Run: func(_ *cobra.Command, _ []string) {
runApp()
},
Version: buildinfo.Version(),
}

func runVisor(args []string) {
func runVisor() {
var ok bool
log := initLogger(tag, syslogAddr)
store, hook := logstore.MakeStore(runtimeLogMaxEntries)
Expand All @@ -276,7 +275,7 @@ func runVisor(args []string) {
stopPProf := initPProf(log, tag, pprofMode, pprofAddr)
defer stopPProf()

conf := initConfig(log, args)
conf := initConfig(log)

if disableHypervisorPKs {
conf.Hypervisors = []cipher.PubKey{}
Expand Down Expand Up @@ -395,7 +394,7 @@ func initPProf(log *logging.MasterLogger, tag string, profMode string, profAddr
return stop
}

func initConfig(mLog *logging.MasterLogger, args []string) *visorconfig.V1 { //nolint
func initConfig(mLog *logging.MasterLogger) *visorconfig.V1 { //nolint
log := mLog.PackageLogger("visor:config")

var r io.Reader
Expand All @@ -416,68 +415,63 @@ func initConfig(mLog *logging.MasterLogger, args []string) *visorconfig.V1 { //n
r = bytes.NewReader(f)
}

conf, err := visorconfig.Reader(r)
conf, compat, err := visorconfig.Parse(log, r)
if err != nil {
log.WithError(err).Fatal("Failed to read in config.")
}
log.WithField("config version: ", conf.Version).Info()

if (conf.Version != "unknown") && (skyenv.BuildInfo.Version != "unknown") {
if compat, err := regexp.MatchString(strings.Split(skyenv.BuildInfo.Version, "-")[0], conf.Version); err == nil {
if !compat {
log.Error("config version does not match visor version")
log.WithField("skywire version: ", skyenv.BuildInfo.Version).Error()
var updstr string
if match, err := regexp.MatchString("/tmp/", skyenv.Skywire); err == nil {
log.Info("match:", match)
if match {
if _, err := os.Stat("cmd/skywire-cli/skywire-cli.go"); err == nil {
updstr = "go run cmd/skywire-cli/skywire-cli.go config gen -b"
}
log.Info("updstr:", updstr)

}
}
if updstr == "" {
updstr = "skywire-cli config gen -b"
}
if conf.Hypervisor != nil {
updstr = updstr + "i"
}
for _, j := range conf.Hypervisors {
if fmt.Sprintf("\t%s\n", j) != "" {
updstr = updstr + "x"
break
}
if !compat {
log.Error("config version does not match visor version")
log.WithField("skywire version: ", skyenv.BuildInfo.Version).Error()
var updstr string
if match := strings.Contains("/tmp/", skyenv.Skywire); err == nil {
log.Info("match:", match)
if match {
if _, err := os.Stat("cmd/skywire-cli/skywire-cli.go"); err == nil {
updstr = "go run cmd/skywire-cli/skywire-cli.go config gen -b"
}
var pkgenv bool
if pkgenv, err = regexp.MatchString("/opt/skywire/apps", conf.Launcher.BinPath); err == nil {
if pkgenv {
updstr = updstr + "p"
}
}
//there is no config *file* with stdin
if skyenv.ConfigName != visorconfig.StdinName {
if _, err = exec.LookPath("stat"); err == nil {
if owner, err := script.Exec(`stat -c '%U' ` + skyenv.ConfigName).String(); err == nil {
if (owner == "root") || (owner == "root\n") {
updstr = "sudo " + updstr
}
}
}
updstr = "\n " + updstr + "ro " + skyenv.ConfigName + "\n"
} else {
updstr = "\n " + updstr + "n" + " | go run cmd/skywire-visor/skywire-visor.go -n"
if launchBrowser {
updstr = updstr + "b"
log.Info("updstr:", updstr)

}
}
if updstr == "" {
updstr = "skywire-cli config gen -b"
}
if conf.Hypervisor != nil {
updstr = updstr + "i"
}
for _, j := range conf.Hypervisors {
if fmt.Sprintf("\t%s\n", j) != "" {
updstr = updstr + "x"
break
}
}

if pkgenv := strings.Contains("/opt/skywire/apps", conf.Launcher.BinPath); err == nil {
if pkgenv {
updstr = updstr + "p"
}
}
//there is no config *file* with stdin
if skyenv.ConfigName != visorconfig.StdinName {
if _, err = exec.LookPath("stat"); err == nil {
if owner, err := script.Exec(`stat -c '%U' ` + skyenv.ConfigName).String(); err == nil {
if (owner == "root") || (owner == "root\n") {
updstr = "sudo " + updstr
}
updstr = updstr + "\n"
}
updstr = "\n " + updstr + "\n"
log.Info("please update your config with the following command:\n", updstr)
log.Fatal("failed to start skywire")
}
updstr = "\n " + updstr + "ro " + skyenv.ConfigName + "\n"
} else {
updstr = "\n " + updstr + "n" + " | go run cmd/skywire-visor/skywire-visor.go -n"
if launchBrowser {
updstr = updstr + "b"
}
updstr = updstr + "\n"
}
updstr = "\n " + updstr + "\n"
log.Info("please update your config with the following command:\n", updstr)
log.Fatal("failed to start skywire")
}
if hypervisorUI {
config := hypervisorconfig.GenerateWorkDirConfig(false)
Expand Down
53 changes: 12 additions & 41 deletions pkg/visor/visorconfig/parse.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package visorconfig

import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"strings"

"github.com/skycoin/skycoin/src/util/logging"

utilenv "github.com/skycoin/skywire-utilities/pkg/skyenv"
"github.com/skycoin/skywire/pkg/skyenv"
)

Expand All @@ -18,46 +16,19 @@ var (
)

// Parse parses the visor config from a given reader.
// If the config file is not the most recent version, it is upgraded and written back to 'path'.
func Parse(log *logging.MasterLogger, raw []byte, options *ParseOptions) (*V1, error) {
// The config version is checked against the visor's version and if not the same we send back the
// error as well as compat(compatibility) as false.
func Parse(log *logging.Logger, r io.Reader) (conf *V1, compat bool, err error) {

cc, err := NewCommon(log, nil)
conf, err = Reader(r)
if err != nil {
return nil, err
return nil, compat, err
}
log.WithField("config version: ", conf.Version).Info()

if err := json.Unmarshal(raw, cc); err != nil {
return nil, fmt.Errorf("failed to obtain config version: %w", err)
// we check if the version of the visor and config are the same
if (conf.Version != "unknown") && (skyenv.BuildInfo.Version != "unknown") {
compat = strings.Contains(strings.Split(skyenv.BuildInfo.Version, "-")[0], strings.Split(conf.Version, "-")[0])
}
return parseV1(cc, raw, options)
}

func parseV1(cc *Common, raw []byte, options *ParseOptions) (*V1, error) {
conf := MakeBaseConfig(cc, options.testEnv, options.dmsgHTTP, options.services)
dec := json.NewDecoder(bytes.NewReader(raw))
if err := dec.Decode(&conf); err != nil {
return nil, err
}

if err := conf.ensureKeys(); err != nil {
return nil, fmt.Errorf("%v: %w", ErrInvalidSK, err)
}
conf = ensureAppDisc(conf)
conf.Version = skyenv.Version()
return conf, conf.flush(conf, options.path)
}

func ensureAppDisc(conf *V1) *V1 {
if conf.Launcher.ServiceDisc == "" {
conf.Launcher.ServiceDisc = utilenv.ServiceDiscAddr
}
return conf
}

// ParseOptions is passed to Parse
type ParseOptions struct {
path string
testEnv bool
dmsgHTTP bool
services *Services
return conf, compat, nil
}
59 changes: 0 additions & 59 deletions pkg/visor/visorconfig/parse_test.go

This file was deleted.

0 comments on commit c50ebe4

Please sign in to comment.