当前位置: 首页>>代码示例>>Golang>>正文


Golang errwrap.Wrapf函数代码示例

本文整理汇总了Golang中github.com/timeglass/glass/_vendor/github.com/hashicorp/errwrap.Wrapf函数的典型用法代码示例。如果您正苦于以下问题:Golang Wrapf函数的具体用法?Golang Wrapf怎么用?Golang Wrapf使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了Wrapf函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: Run

func (c *Init) Run(ctx *cli.Context) error {
	c.Println("Writing version control hooks...")
	dir, err := os.Getwd()
	if err != nil {
		return errwrap.Wrapf("Failed to fetch current working dir: {{err}}", err)
	}

	vc, err := vcs.GetVCS(dir)
	if err != nil {
		return errwrap.Wrapf("Failed to setup VCS: {{err}}", err)
	}

	err = vc.Hook()
	if err != nil {
		return errwrap.Wrapf("Failed to write hooks: {{err}}", err)
	}

	c.Println("Hooks written!")
	err = NewStart().Run(ctx)
	if err != nil {
		return err
	}

	err = NewPull().Run(ctx)
	if err != nil {
		if errwrap.Contains(err, vcs.ErrNoRemote.Error()) {
			c.Println("No remote found, skipping pull")
		} else {
			return err
		}
	}

	return nil
}
开发者ID:rosetree,项目名称:glass,代码行数:34,代码来源:init.go

示例2: Load

func (k *Keeper) Load() error {
	f, err := os.Open(k.ledgerPath)
	if err != nil {
		if !os.IsNotExist(err) {
			return errwrap.Wrapf(fmt.Sprintf("Failed to open '%s': {{err}}", k.ledgerPath), err)
		}
	} else {
		defer f.Close()
		dec := json.NewDecoder(f)
		err := dec.Decode(k)
		if err != nil {
			return errwrap.Wrapf(fmt.Sprintf("Failed to decode JSON in '%s': {{err}}", k.ledgerPath), err)
		}

		//immediately restart and link save channel if not paused
		for _, t := range k.keeperData.Timers {
			t.SetSave(k.save)
			if !t.IsPaused() {
				t.Start()
			}
		}

	}

	return nil
}
开发者ID:rosetree,项目名称:glass,代码行数:26,代码来源:keeper.go

示例3: Run

func (c *Pull) Run(ctx *cli.Context) error {
	dir, err := os.Getwd()
	if err != nil {
		return errwrap.Wrapf("Failed to fetch current working dir: {{err}}", err)
	}

	vc, err := vcs.GetVCS(dir)
	if err != nil {
		return errwrap.Wrapf("Failed to setup VCS: {{err}}", err)
	}

	remote := ctx.Args().First()
	if remote == "" {
		remote, err = vc.DefaultRemote()
		if err != nil {
			return errwrap.Wrapf("Failed to determine default remote: {{err}}", err)
		}
	}

	err = vc.Pull(remote)
	if err != nil {
		if err == vcs.ErrNoRemoteTimeData {
			c.Printf("Remote '%s' has no time data (yet), nothing to pull\n", remote)
			return nil
		}

		return errwrap.Wrapf("Failed to pull time data: {{err}}", err)
	}

	c.Println("Time data was pulled successfully")
	return nil
}
开发者ID:rosetree,项目名称:glass,代码行数:32,代码来源:pull.go

示例4: timersCreate

func (s *Server) timersCreate(w http.ResponseWriter, r *http.Request) {
	err := r.ParseForm()
	if err != nil {
		s.Respond(w, err)
		return
	}

	if dirs, ok := r.Form["dir"]; !ok {
		s.Respond(w, fmt.Errorf("dir parameter is mandatory"))
		return
	} else {
		for _, dir := range dirs {
			t, err := NewTimer(dir)
			if err != nil {
				s.Respond(w, errwrap.Wrapf("Failed to create new timer: {{err}}", err))
				return
			}

			err = s.keeper.Add(t)
			if err != nil {
				s.Respond(w, errwrap.Wrapf("Failed to add new timer to keeper: {{err}}", err))
				return
			}
		}
	}

	w.WriteHeader(http.StatusCreated)
}
开发者ID:rosetree,项目名称:glass,代码行数:28,代码来源:server.go

示例5: Run

func (c *Install) Run(ctx *cli.Context) error {
	c.Println("Installing the Timeglass background service...")

	//attempt to install
	cmd := exec.Command("glass-daemon", "install")
	cmd.Stderr = os.Stderr
	cmd.Stdout = os.Stdout

	err := cmd.Run()
	if err != nil {
		return errwrap.Wrapf(fmt.Sprintf("Failed to install Daemon: {{err}}"), err)
	}

	c.Println("Starting the Timeglass background service...")

	//attempt to start
	cmd = exec.Command("glass-daemon", "start")
	cmd.Stderr = os.Stderr
	cmd.Stdout = os.Stdout
	err = cmd.Run()
	if err != nil {
		return errwrap.Wrapf(fmt.Sprintf("Failed to start Daemon: {{err}}"), err)
	}

	c.Println("Done!")
	return nil
}
开发者ID:rosetree,项目名称:glass,代码行数:27,代码来源:install.go

示例6: Run

func (c *Sum) Run(ctx *cli.Context) error {
	dir, err := os.Getwd()
	if err != nil {
		return errwrap.Wrapf("Failed to fetch current working dir: {{err}}", err)
	}

	//write the vcs
	vc, err := vcs.GetVCS(dir)
	if err != nil {
		return errwrap.Wrapf("Failed to setup VCS: {{err}}", err)
	}

	//retrieve commits through piped stdin or arguments
	commits := []string{}
	if !isatty.IsTerminal(os.Stdin.Fd()) {
		scanner := bufio.NewScanner(os.Stdin)
		for scanner.Scan() {
			commits = append(commits, scanner.Text())
		}
		if err := scanner.Err(); err != nil {
			c.Println(err)
		}
	} else {
		if ctx.Args().First() != "" {
			commits = append(commits, ctx.Args().First())
		}
		commits = append(commits, ctx.Args().Tail()...)
	}

	if len(commits) == 0 {
		return fmt.Errorf("Please provide at least one commit through STDIN or as an argument.")
	}

	//map time data from the vcs
	list := []vcs.TimeData{}
	for _, c := range commits {
		data, err := vc.Show(c)
		if err != nil {
			if err == vcs.ErrNoCommitTimeData {
				//ignore if a commit has no time attached
				continue
			}

			return errwrap.Wrapf(fmt.Sprintf("Failed to show time notes for '%s': {{err}}", c), err)
		}

		list = append(list, data)
	}

	//reduce to output
	var total time.Duration
	for _, data := range list {
		total += data.Total()
	}

	fmt.Fprintln(os.Stdout, total)
	return nil
}
开发者ID:rosetree,项目名称:glass,代码行数:58,代码来源:sum.go

示例7: timersInfo

func (s *Server) timersInfo(w http.ResponseWriter, r *http.Request) {
	err := r.ParseForm()
	if err != nil {
		s.Respond(w, err)
		return
	}

	timers := []*Timer{}
	if dirs, ok := r.Form["dir"]; !ok {
		s.Respond(w, fmt.Errorf("dir parameter is mandatory"))
		return
	} else {
		for _, dir := range dirs {
			t, err := s.keeper.Get(dir)
			if err != nil {
				s.Respond(w, errwrap.Wrapf("Failed to get timer: {{err}}", err))
				return
			}

			timers = append(timers, t)
		}
	}

	s.Respond(w, timers)
}
开发者ID:rosetree,项目名称:glass,代码行数:25,代码来源:server.go

示例8: Call

func (c *Client) Call(method string, params url.Values) ([]byte, error) {
	loc := fmt.Sprintf("%s/api/%s?%s", c.endpoint, method, params.Encode())
	resp, err := c.Get(loc)
	if err != nil {
		return nil, ErrRequestFailed
	}

	body := bytes.NewBuffer(nil)
	defer resp.Body.Close()
	_, err = io.Copy(body, resp.Body)
	if err != nil {
		return body.Bytes(), errwrap.Wrapf(fmt.Sprintf("Failed to buffer response body: {{err}}"), err)
	}

	if resp.StatusCode > 299 {
		errresp := &struct {
			Error string
		}{}

		err := json.Unmarshal(body.Bytes(), &errresp)
		if err != nil || errresp.Error == "" {
			return body.Bytes(), fmt.Errorf("Unexpected StatusCode returned from Deamon: '%d', body: '%s'", resp.StatusCode, body.String())
		} else if strings.Contains(errresp.Error, "No known timer") {
			return body.Bytes(), ErrTimerNotFound
		}

		return body.Bytes(), fmt.Errorf(errresp.Error)
	}

	return body.Bytes(), nil
}
开发者ID:rosetree,项目名称:glass,代码行数:31,代码来源:client.go

示例9: Stop

func (t *Timer) Stop() {
	if !t.running {
		return
	}

	if t.monitor != nil {

		//@todo Remove this at some point, it normalizes
		//time after rapid stop start usage on OSX
		//for unkown reasons, long term solution should probably
		//involve some mechanism that prevents the darwin monitor
		//form stopping to quickly after being started
		<-time.After(time.Millisecond)

		err := t.monitor.Stop()
		if err != nil {
			log.Print(errwrap.Wrapf("Failed to stop monitor: {{err}}", err))
		}

		t.monitor = nil
	}

	t.stopto <- struct{}{}
	t.stoptick <- struct{}{}

	t.timerData.Paused = true
	t.running = false
}
开发者ID:rosetree,项目名称:glass,代码行数:28,代码来源:timer.go

示例10: Run

func (c *Punch) Run(ctx *cli.Context) error {
	dir, err := os.Getwd()
	if err != nil {
		return errwrap.Wrapf("Failed to fetch current working dir: {{err}}", err)
	}

	var input string
	if isatty.IsTerminal(os.Stdin.Fd()) {
		c.Println("Reading input from argument...")
		input = ctx.Args().First()
	} else {
		c.Println("Reading input from Stdin...")
		bytes, err := ioutil.ReadAll(os.Stdin)
		if err != nil {
			return errwrap.Wrapf("Failed to read time from Stdin: {{err}}", err)
		}
		input = string(bytes)
	}

	if input == "" {
		return fmt.Errorf("Please provide the time you spent as the first argument")
	}

	t, err := time.ParseDuration(input)
	if err != nil {
		return errwrap.Wrapf(fmt.Sprintf("Failed to parse provided argument '%s' as a valid duration (e.g 1h2m10s): {{err}}", input), err)
	}

	//write the vcs
	vc, err := vcs.GetVCS(dir)
	if err != nil {
		return errwrap.Wrapf("Failed to setup VCS: {{err}}", err)
	}

	c.Printf("Persisting %s to version control...", t)
	err = vc.Persist(t)
	if err != nil {
		return errwrap.Wrapf("Failed to log time into VCS: {{err}}", err)
	}

	c.Println("Done!")
	return nil
}
开发者ID:rosetree,项目名称:glass,代码行数:43,代码来源:punch.go

示例11: Run

func (c *Start) Run(ctx *cli.Context) error {
	dir, err := os.Getwd()
	if err != nil {
		return errwrap.Wrapf("Failed to fetch current working dir: {{err}}", err)
	}

	vc, err := vcs.GetVCS(dir)
	if err != nil {
		return errwrap.Wrapf("Failed to setup VCS: {{err}}", err)
	}

	c.Println("Starting timer...")

	client := NewClient()
	err = client.CreateTimer(vc.Root())
	if err != nil {
		return errwrap.Wrapf("Failed to create timer: {{err}}", err)
	}

	c.Println("Timer started!")
	return nil
}
开发者ID:rosetree,项目名称:glass,代码行数:22,代码来源:start.go

示例12: Run

func (c *Pause) Run(ctx *cli.Context) error {
	dir, err := os.Getwd()
	if err != nil {
		return errwrap.Wrapf("Failed to fetch current working dir: {{err}}", err)
	}

	vc, err := vcs.GetVCS(dir)
	if err != nil {
		return errwrap.Wrapf("Failed to setup VCS: {{err}}", err)
	}

	c.Printf("Pausing timer...")

	client := NewClient()
	err = client.PauseTimer(vc.Root())
	if err != nil {
		return errwrap.Wrapf(fmt.Sprintf("Failed to pause timer: {{err}}"), err)
	}

	c.Printf("Done!")
	return nil
}
开发者ID:rosetree,项目名称:glass,代码行数:22,代码来源:pause.go

示例13: SystemTimeglassPathCreateIfNotExist

func SystemTimeglassPathCreateIfNotExist() (string, error) {
	path, err := SystemTimeglassPath()
	if err != nil {
		return "", err
	}

	err = os.MkdirAll(path, 0755)
	if err != nil {
		return "", errwrap.Wrapf(fmt.Sprintf("Failed to create Timeglass system dir '%s': {{err}}", path), err)
	}

	return path, nil
}
开发者ID:rosetree,项目名称:glass,代码行数:13,代码来源:paths.go

示例14: Info

func (c *Client) Info() (map[string]interface{}, error) {
	data, err := c.Call("", url.Values{})
	if err != nil {
		return nil, err
	}

	v := map[string]interface{}{}
	err = json.Unmarshal(data, &v)
	if err != nil {
		return nil, errwrap.Wrapf(fmt.Sprintf("Failed to deserialize '%s' into map: {{err}}", data), err)
	}

	return v, nil
}
开发者ID:rosetree,项目名称:glass,代码行数:14,代码来源:client.go

示例15: UnmarshalJSON

func (t *MBU) UnmarshalJSON(data []byte) error {
	raw, err := strconv.Unquote(string(data))
	if err != nil {
		return err
	}

	parsed, err := time.ParseDuration(raw)
	if err != nil {
		return errwrap.Wrapf("Failed to parse duration: {{err}}", err)
	}

	*t = MBU(parsed)
	return nil
}
开发者ID:rosetree,项目名称:glass,代码行数:14,代码来源:config.go


注:本文中的github.com/timeglass/glass/_vendor/github.com/hashicorp/errwrap.Wrapf函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。