Esempio n. 1
0
func installCommand(args []string) {
	fs := flag.NewFlagSet("install", flag.ExitOnError)
	fs.Usage = usageFor(fs, msgInstallUsage)
	fs.Parse(args)
	args = fs.Args()

	cl := NewClient(serverAddress(), moleIni.Get("server", "fingerprint"))
	if len(args) == 0 {
		pkgMap, err := cl.Packages()
		fatalErr(err)

		arch := runtime.GOOS + "-" + runtime.GOARCH
		var rows [][]string
		for _, pkg := range pkgMap[arch] {
			rows = append(rows, []string{pkg.Package, pkg.Description})
		}

		if len(rows) > 0 {
			rows = append([][]string{{"PKG", "DESCRIPTION"}}, rows...)
			infoln(table.FmtFunc("ll", rows, tableFormatter))
		} else {
			infoln(msgNoPackages)
		}
	} else {
		requireRoot("install")
		name := args[0]
		fullname := args[0] + "-" + runtime.GOOS + "-" + runtime.GOARCH + ".tar.gz"
		wr, err := ioutil.TempFile("", name)
		fatalErr(err)

		rd, err := cl.Package(fullname)
		fatalErr(err)

		_, err = io.Copy(wr, rd)
		fatalErr(err)
		err = wr.Close()
		fatalErr(err)
		_ = rd.Close()

		td, err := ioutil.TempDir("", name)
		fatalErr(err)

		cmd := exec.Command("sh", "-c", "cd "+td+" && tar zxf "+wr.Name())
		_, err = cmd.CombinedOutput()
		fatalErr(err)

		becomeRoot()
		cmd = exec.Command(td+"/install.sh", td)
		_, err = cmd.CombinedOutput()
		fatalErr(err)
		dropRoot()

		okln("Installed")
	}
}
Esempio n. 2
0
File: cmd-ls.go Progetto: calmh/mole
func commandLs(args []string) {
	fs := flag.NewFlagSet("ls", flag.ContinueOnError)
	short := fs.Bool("s", false, "Short listing")
	long := fs.Bool("l", false, "Long listing")
	fs.Usage = usageFor(fs, msgLsUsage)
	err := fs.Parse(args)
	if err != nil {
		fmt.Println(ansi.Bold("Feature Flags:"))
		fmt.Println(msgLsFlags)
		exit(3)
	}
	args = fs.Args()

	var re *regexp.Regexp
	if len(args) == 1 {
		re, err = regexp.Compile("(?i)" + args[0])
		fatalErr(err)
	}

	cl := NewClient(serverAddress(), moleIni.Get("server", "fingerprint"))
	res, err := authenticated(cl, func() (interface{}, error) { return cl.List() })
	fatalErr(err)
	l := res.([]ListItem)

	var rows [][]string
	var header []string
	var format string
	var hasFeatureFlags bool

	for _, i := range l {
		if i.Features != 0 {
			hasFeatureFlags = true
			break
		}
	}

	if hasFeatureFlags {
		header = []string{"TUNNEL", "FLAGS", "DESCRIPTION"}
		format = "lll"
	} else {
		header = []string{"TUNNEL", "DESCRIPTION"}
		format = "ll"
	}
	if *long {
		header = append(header, "HOSTS", "VER")
		format += "lr"
	}

	rows = [][]string{header}

	tunnelCache, _ := os.Create(path.Join(homeDir, "tunnels.cache"))

	var matched int
	for _, i := range l {
		hosts := strings.Join(i.Hosts, ", ")
		if re == nil || re.MatchString(i.Name) || re.MatchString(i.Description) || re.MatchString(hosts) {
			if tunnelCache != nil {
				fmt.Fprintln(tunnelCache, i.Name)
			}

			if *short {
				fmt.Println(i.Name)
			} else {
				matched++

				row := []string{i.Name}

				if hasFeatureFlags {
					flags := ""
					spacer := "·"
					unsupported := i.Features & ^(conf.FeatureError|conf.FeatureSshKey|conf.FeatureSshPassword|conf.FeatureLocalOnly|conf.FeatureVpnc|conf.FeatureOpenConnect|conf.FeatureSocks) != 0

					if i.Features&conf.FeatureError != 0 {
						flags = strings.Repeat(spacer, 4) + "E"
					} else {
						if i.Features&conf.FeatureVpnc != 0 {
							flags += "v"
							unsupported = unsupported || !supportsVpn("vpnc")
						} else if i.Features&conf.FeatureOpenConnect != 0 {
							flags += "o"
							unsupported = unsupported || !supportsVpn("openconnect")
						} else {
							flags += spacer
						}

						if i.Features&conf.FeatureSshKey != 0 {
							flags += "k"
						} else {
							flags += spacer
						}

						if i.Features&conf.FeatureSshPassword != 0 {
							flags += "p"
						} else {
							flags += spacer
						}

						if i.Features&conf.FeatureSocks != 0 {
							flags += "s"
						} else if i.Features&conf.FeatureLocalOnly != 0 {
							flags += "l"
						} else {
							flags += spacer
						}

						if unsupported {
							flags += "U"
						} else {
							flags += spacer
						}
					}
					row = append(row, flags)
				}

				row = append(row, i.Description)

				if *long {
					ver := fmt.Sprintf("%.01f", i.Version)
					if hosts == "" {
						hosts = "-"
					}
					row = append(row, hosts, ver)
				}

				rows = append(rows, row)
			}
		}
	}

	if tunnelCache != nil {
		_ = tunnelCache.Close()
	}

	if !*short {
		// Never prefix table with log stuff
		fmt.Printf(table.FmtFunc(format, rows, tableFormatter))
		if matched != len(l) {
			fmt.Printf(ansi.Faint(" - Matched %d out of %d records\n"), matched, len(l))
		}
	}
}