Exemplo n.º 1
0
Arquivo: cli.go Projeto: get3w/get3w
// NewGet3WCli returns a Get3WCli instance with IO output and error streams set by in, out and err.
// The key file, protocol (i.e. unix) and address are passed in as strings, along with the tls.Config. If the tls.Config
// is set the client scheme will be set to https.
// The client will be given a 32-second timeout (see https://github.com/docker/docker/pull/8035).
func NewGet3WCli(in io.ReadCloser, out, err io.Writer, clientFlags *cli.ClientFlags) *Get3WCli {
	cli := &Get3WCli{
		in:      in,
		out:     out,
		err:     err,
		keyFile: clientFlags.Common.TrustKey,
	}

	cli.init = func() error {

		clientFlags.PostParse()

		if cli.in != nil {
			cli.inFd, cli.isTerminalIn = term.GetFdInfo(cli.in)
		}
		if cli.out != nil {
			cli.outFd, cli.isTerminalOut = term.GetFdInfo(cli.out)
		}

		config, err := home.LoadConfig()
		if err != nil {
			fmt.Fprintf(cli.err, "WARNING: Error loading config file:%v\n", err)
		}
		cli.config = config

		return nil
	}

	return cli
}
Exemplo n.º 2
0
Arquivo: login.go Projeto: get3w/get3w
// Login user login
func Login() echo.HandlerFunc {
	return func(c echo.Context) error {
		input := &get3w.UserLoginInput{}
		err := api.LoadRequestInput(c, input)
		if err != nil {
			return api.ErrorBadRequest(c, err)
		}

		if input.Account == "" || input.Password == "" {
			return api.ErrorUnauthorized(c, nil)
		}

		client := get3w.NewClient("")
		output, _, err := client.Users.Login(input)
		if err != nil {
			return api.ErrorUnauthorized(c, err)
		}

		config, _ := home.LoadConfig()
		config.AuthConfig = home.AuthConfig{
			Username:    output.User.Username,
			Password:    input.Password,
			AccessToken: output.AccessToken,
		}

		if err := config.Save(); err != nil {
			return api.ErrorInternal(c, fmt.Errorf("ERROR: failed to save config file: %v", err))
		}

		return c.JSON(http.StatusOK, output)
	}
}
Exemplo n.º 3
0
Arquivo: api.go Projeto: get3w/get3w
// Config returns home.Config
func Config(c echo.Context) *home.Config {
	config := c.Get("Config")
	if config != nil {
		return config.(*home.Config)
	}
	load, _ := home.LoadConfig()
	return load
}
Exemplo n.º 4
0
// Logout user logout
func Logout() echo.HandlerFunc {
	return func(c echo.Context) error {
		config, _ := home.LoadConfig()
		err := config.Logout()
		if err != nil {
			return api.ErrorInternal(c, err)
		}

		return c.JSON(http.StatusOK, map[string]interface{}{})
	}
}
Exemplo n.º 5
0
Arquivo: api.go Projeto: get3w/get3w
// GetApp returns app by path
func GetApp(path string) (*get3w.App, error) {
	config, err := home.LoadConfig()
	if err != nil {
		return nil, err
	}
	var app *get3w.App
	for _, localApp := range config.Apps {
		if localApp.Path == path {
			app = localApp
		}
	}
	return app, nil
}
Exemplo n.º 6
0
Arquivo: list.go Projeto: get3w/get3w
// List return apps
func List() echo.HandlerFunc {
	return func(c echo.Context) error {
		if api.IsAnonymous(c) {
			return api.ErrorUnauthorized(c, nil)
		}

		config, err := home.LoadConfig()
		if err != nil {
			return api.ErrorInternal(c, err)
		}

		return c.JSON(http.StatusOK, config.Apps)
	}
}
Exemplo n.º 7
0
Arquivo: api.go Projeto: get3w/get3w
// IsAnonymous return true if no authentication information in the header
func IsAnonymous(c echo.Context) bool {
	accessToken := c.Get("AccessToken").(string)
	if accessToken == "" {
		return true
	}
	config, err := home.LoadConfig()
	if err != nil {
		return true
	}
	if config.AuthConfig.AccessToken != accessToken {
		return true
	}
	return false
}
Exemplo n.º 8
0
// Delete app
func Delete() echo.HandlerFunc {
	return func(c echo.Context) error {
		appPath := c.Param("app_path")
		if appPath == "" {
			return api.ErrorNotFound(c, nil)
		}

		if api.IsAnonymous(c) {
			return api.ErrorUnauthorized(c, nil)
		}

		input := &get3w.AppDeleteInput{}
		err := api.LoadRequestInput(c, input)
		if err != nil {
			return api.ErrorBadRequest(c, err)
		}

		config, err := home.LoadConfig()
		var appToDelete *get3w.App
		index := -1
		for i, app := range config.Apps {
			if app.Path == appPath {
				appToDelete = app
				index = i
				break
			}
		}
		if appToDelete == nil {
			return api.ErrorNotFound(c, nil)
		}

		if !input.KeepFiles {
			err = os.RemoveAll(appPath)
			if err != nil {
				return api.ErrorBadRequest(c, err)
			}
		}

		config.Apps = append(config.Apps[:index], config.Apps[index+1:]...)
		config.Save()

		return c.JSON(http.StatusOK, appToDelete)
	}
}
Exemplo n.º 9
0
Arquivo: sync.go Projeto: get3w/get3w
// Sync app
func Sync() echo.HandlerFunc {
	return func(c echo.Context) error {
		appPath := c.Param("app_path")
		if appPath == "" {
			return api.ErrorNotFound(c, nil)
		}

		if api.IsAnonymous(c) {
			return api.ErrorUnauthorized(c, nil)
		}

		app, err := api.GetApp(appPath)
		if err != nil {
			return api.ErrorInternal(c, err)
		}
		if app == nil {
			return api.ErrorNotFound(c, nil)
		}

		config, err := home.LoadConfig()
		if err != nil {
			return api.ErrorInternal(c, err)
		}

		parser, err := storage.NewLocalParser(api.Owner(c), appPath)
		if err != nil {
			return api.ErrorInternal(c, err)
		}

		buffer := bytes.NewBufferString("")
		shouldLogin, err := parser.Push(&config.AuthConfig, buffer)
		if shouldLogin {
			return api.ErrorUnauthorized(c, nil)
		}
		if err != nil {
			return api.ErrorInternal(c, err)
		}

		return c.JSON(http.StatusOK, &get3w.AppSyncOutput{
			Log: strings.Replace(buffer.String(), "\n", "<br />", -1),
		})
	}
}
Exemplo n.º 10
0
Arquivo: api.go Projeto: get3w/get3w
// StoreHeaders get header values and set to context
func StoreHeaders(options ...*StoreHeaderOptions) echo.MiddlewareFunc {
	return func(next echo.Handler) echo.Handler {
		return echo.HandlerFunc(func(c echo.Context) error {
			request := c.Request()
			header := request.Header()
			url := request.URL()

			//application/vnd.get3w.v3+json
			version := VersionV1
			accept := header.Get("Accept")
			if accept == "" || accept == "application/vnd.get3w.v1+json" {
				version = VersionV1
			}
			c.Set("Version", version)

			auth := header.Get("Authorization")
			l := len(Bearer)
			accessToken := ""

			if len(auth) > l+1 && auth[:l] == Bearer {
				accessToken = auth[l+1:]
			} else if len(header.Get(TokenNameOfHeader)) > 0 {
				accessToken = header.Get(TokenNameOfHeader)
			} else if len(url.QueryValue(TokenNameOfQuery)) > 0 {
				accessToken = url.QueryValue(TokenNameOfQuery)
			}

			c.Set("AccessToken", accessToken)

			config, _ := home.LoadConfig()
			c.Set("Config", config)

			if err := next.Handle(c); err != nil {
				c.Error(err)
			}

			return nil
		})
	}
}
Exemplo n.º 11
0
Arquivo: add.go Projeto: get3w/get3w
// Add app, for open and clone operation
func Add() echo.HandlerFunc {
	return func(c echo.Context) error {
		if api.IsAnonymous(c) {
			return api.ErrorUnauthorized(c, nil)
		}
		owner := api.Owner(c)

		input := &get3w.AppAddInput{}
		err := api.LoadRequestInput(c, input)
		if err != nil {
			return api.ErrorBadRequest(c, err)
		}

		dirPath := input.DirPath
		dirExists := true
		if dirPath == "" {
			dirExists = false
		} else {
			stat, err := os.Lstat(dirPath)
			if err != nil {
				dirExists = false
			} else if !stat.IsDir() {
				dirExists = false
			}
		}
		if !dirExists {
			return api.ErrorNotFound(c, nil)
		}

		config, err := home.LoadConfig()
		if err != nil {
			return api.ErrorBadRequest(c, err)
		}

		success := true
		configExists := true
		var app *get3w.App

		var appPath string
		if input.Origin != "" {
			appPath, err = addFromCloud(dirPath, input.Origin, &config.AuthConfig)
			if err != nil {
				return api.ErrorBadRequest(c, err)
			}
		} else {
			appPath, configExists, err = addFromLocal(dirPath, config)
			if err != nil {
				return api.ErrorBadRequest(c, err)
			}
		}

		if input.Check && !configExists {
			success = false
		}

		if success {
			parser, err := storage.NewLocalParser(config.AuthConfig.Username, appPath)
			if err != nil {
				return api.ErrorInternal(c, err)
			}

			app = &get3w.App{
				Owner:       owner,
				Name:        parser.Name,
				Description: parser.Config.Description,
				Tags:        "",
				Path:        appPath,
				Private:     false,
				CreatedAt:   timeutils.ToString(time.Now()),
				UpdatedAt:   timeutils.ToString(time.Now()),
			}

			exists := false
			for _, app := range config.Apps {
				if app.Path == appPath {
					exists = true
					break
				}
			}
			if !exists {
				config.Apps = append(config.Apps, app)
				config.Save()
			}
		}

		output := &get3w.AppAddOutput{
			Config: configExists,
			App:    app,
		}
		return c.JSON(http.StatusOK, output)
	}
}