Exemplo n.º 1
0
func work(p string) {

	ct = ct + 1

	var level, v int
	//fmt.Println(p)

	inf, err := os.Stat(p)
	if err != nil {
		return
	}

	segments := strings.Split(p, "/")
	w1 := strings.ToLower(segments[len(segments)-1])

	if strings.HasPrefix(w1, ".") {
		return
	}

	if w1 == q {
		v = 0
	} else if strings.HasPrefix(w1, q) {
		v = 1
	} else if strings.Contains(w1, q) {
		v = 2
	} else {
		v = fuzzy.Levenshtein(&w1, &q)
	}
	// if umbral
	if v <= 2 {
		if inf.IsDir() {
			level = len(segments)
		} else {
			level = len(segments) - 1
		}

		pa := Result{w1, v, level, p}
		result = append(result, pa)
	}

}
Exemplo n.º 2
0
Arquivo: cmd.go Projeto: 4eek/tsuru
func (m *Manager) Run(args []string) {
	var (
		status         int
		verbosity      int
		displayHelp    bool
		displayVersion bool
	)
	if len(args) == 0 {
		args = append(args, "help")
	}
	flagset := gnuflag.NewFlagSet("tsuru flags", gnuflag.ContinueOnError)
	flagset.SetOutput(m.stderr)
	flagset.IntVar(&verbosity, "verbosity", 0, "Verbosity level: 1 => print HTTP requests; 2 => print HTTP requests/responses")
	flagset.IntVar(&verbosity, "v", 0, "Verbosity level: 1 => print HTTP requests; 2 => print HTTP requests/responses")
	flagset.BoolVar(&displayHelp, "help", false, "Display help and exit")
	flagset.BoolVar(&displayHelp, "h", false, "Display help and exit")
	flagset.BoolVar(&displayVersion, "version", false, "Print version and exit")
	parseErr := flagset.Parse(false, args)
	if parseErr != nil {
		fmt.Fprint(m.stderr, parseErr)
		m.finisher().Exit(2)
		return
	}
	args = flagset.Args()
	if displayHelp {
		args = append([]string{"help"}, args...)
	} else if displayVersion {
		args = []string{"version"}
	}
	name := args[0]
	command, ok := m.Commands[name]
	if !ok {
		if m.lookup != nil {
			context := m.newContext(args, m.stdout, m.stderr, m.stdin)
			err := m.lookup(context)
			if err != nil {
				msg := ""
				if os.IsNotExist(err) {
					msg = fmt.Sprintf("%s: %q is not a tsuru command. See %q.\n", os.Args[0], args[0], "tsuru help")
					msg += fmt.Sprintf("\nDid you mean?\n")
					var keys []string
					for key := range m.Commands {
						keys = append(keys, key)
					}
					sort.Strings(keys)
					for _, key := range keys {
						levenshtein := fuzzy.Levenshtein(&key, &args[0])
						if levenshtein < 3 || strings.Contains(key, args[0]) {
							msg += fmt.Sprintf("\t%s\n", key)
						}
					}
				} else {
					msg = err.Error()
				}
				fmt.Fprint(m.stderr, msg)
				m.finisher().Exit(1)
			}
			return
		}
		fmt.Fprintf(m.stderr, "Error: command %q does not exist\n", args[0])
		m.finisher().Exit(1)
		return
	}
	args = args[1:]
	info := command.Info()
	command, args, err := m.handleFlags(command, name, args)
	if err != nil {
		fmt.Fprint(m.stderr, err)
		m.finisher().Exit(1)
		return
	}
	if length := len(args); (length < info.MinArgs || (info.MaxArgs > 0 && length > info.MaxArgs)) &&
		name != "help" {
		m.wrong = true
		m.original = info.Name
		command = m.Commands["help"]
		args = []string{name}
		status = 1
	}
	context := m.newContext(args, m.stdout, m.stderr, m.stdin)
	client := NewClient(&http.Client{}, context, m)
	client.Verbosity = verbosity
	err = command.Run(context, client)
	if err == errUnauthorized && name != "login" {
		loginCmdName := "login"
		if cmd, ok := m.Commands[loginCmdName]; ok {
			fmt.Fprintln(m.stderr, "Error: you're not authenticated or your session has expired.")
			fmt.Fprintf(m.stderr, "Calling the %q command...\n", loginCmdName)
			loginContext := m.newContext(nil, m.stdout, m.stderr, m.stdin)
			if err = cmd.Run(loginContext, client); err == nil {
				fmt.Fprintln(m.stderr)
				err = command.Run(context, client)
			}
		}
	}
	if err != nil {
		errorMsg := err.Error()
		httpErr, ok := err.(*errors.HTTP)
		if ok && httpErr.Code == http.StatusUnauthorized && name != "login" {
			errorMsg = `You're not authenticated or your session has expired. Please use "login" command for authentication.`
		}
		if !strings.HasSuffix(errorMsg, "\n") {
			errorMsg += "\n"
		}
		if err != ErrAbortCommand {
			io.WriteString(m.stderr, "Error: "+errorMsg)
		}
		status = 1
	}
	m.finisher().Exit(status)
}
Exemplo n.º 3
0
func (m *Manager) Run(args []string) {
	var (
		status         int
		verbosity      bool
		displayHelp    bool
		displayVersion bool
	)
	if len(args) == 0 {
		args = append(args, "help")
	}
	flagset := gnuflag.NewFlagSet("megamd flags", gnuflag.ContinueOnError)
	flagset.SetOutput(m.stderr)
	flagset.BoolVar(&verbosity, "verbosity", false, "Verbosity print debug")
	flagset.BoolVar(&verbosity, "v", false, "Verbosity print debug")
	flagset.BoolVar(&displayHelp, "help", false, "Display help and exit")
	flagset.BoolVar(&displayHelp, "h", false, "Display help and exit")
	flagset.BoolVar(&displayVersion, "version", false, "Print version and exit")
	parseErr := flagset.Parse(false, args)
	if parseErr != nil {
		fmt.Fprint(m.stderr, parseErr)
		m.finisher().Exit(2)
		return
	}
	args = flagset.Args()
	if displayHelp {
		args = append([]string{"help"}, args...)
	} else if displayVersion {
		args = []string{"version"}
	}
	name := args[0]
	command, ok := m.Commands[name]
	if !ok {
		if m.lookup != nil {
			context := m.newContext(args, m.stdout, m.stderr, m.stdin)
			err := m.lookup(context)
			if err != nil {
				msg := ""
				if os.IsNotExist(err) {
					msg = fmt.Sprintf("%s: %q is not a megamd command. See %q.\n", os.Args[0], args[0], "megamd help")
					var keys []string
					for key := range m.Commands {
						keys = append(keys, key)
					}
					sort.Strings(keys)
					for _, key := range keys {
						levenshtein := fuzzy.Levenshtein(&key, &args[0])
						if levenshtein < 3 || strings.Contains(key, args[0]) {
							if !strings.Contains(msg, "Did you mean?") {
								msg += fmt.Sprintf("\nDid you mean?\n")
							}
							msg += fmt.Sprintf("\t%s\n", key)
						}
					}
				} else {
					msg = err.Error()
				}
				fmt.Fprint(m.stderr, msg)
				m.finisher().Exit(1)
			}
			return
		}
		fmt.Fprintf(m.stderr, "Error: command %q does not exist\n", args[0])
		m.finisher().Exit(1)
		return
	}
	args = args[1:]
	info := command.Info()
	command, args, err := m.handleFlags(command, name, args)
	if err != nil {
		fmt.Fprint(m.stderr, err)
		m.finisher().Exit(1)
		return
	}
	if length := len(args); (length < info.MinArgs || (info.MaxArgs > 0 && length > info.MaxArgs)) &&
		name != "help" {
		m.wrong = true
		m.original = info.Name
		command = m.Commands["help"]
		args = []string{name}
		status = 1
	}
	context := m.newContext(args, m.stdout, m.stderr, m.stdin)
	if m.mode != nil && verbosity {
		m.mode(2)
	}

	err = command.Run(context)
	if err != nil {
		errorMsg := err.Error()
		if !strings.HasSuffix(errorMsg, "\n") {
			errorMsg += "\n"
		}
		if err != ErrAbortCommand {
			io.WriteString(m.stderr, "Error: "+errorMsg)
		}
		status = 1
	}

	m.finisher().Exit(status)
}