• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang errwrap.Wrapf函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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



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


示例16: Start

func (p *daemon) Start(s service.Service) error {
	var err error

	path, err := SystemTimeglassPathCreateIfNotExist()
	if err != nil {
		return errwrap.Wrapf("Failed to find Timeglass system path: {{err}}", err)
	}

	p.keeper, err = NewKeeper(path)
	if err != nil {
		return errwrap.Wrapf("Failed to create time keeper: {{err}}", err)
	}

	bind := "127.0.0.1:3838"
	p.server, err = NewServer(bind, p.keeper)
	if err != nil {
		return errwrap.Wrapf(fmt.Sprintf("Failed to create server on '%s': {{err}}, is the service already running?", bind), err)
	}

	go p.server.checkVersion()
	go p.keeper.Start()
	go p.run()
	return nil
}
开发者ID:rosetree,项目名称:glass,代码行数:24,代码来源:main.go


示例17: Save

func (k *Keeper) Save() error {
	f, err := os.OpenFile(k.ledgerPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666)
	if err != nil {
		return err
	}
	defer f.Close()

	enc := json.NewEncoder(f)
	err = enc.Encode(k)
	if err != nil {
		return errwrap.Wrapf(fmt.Sprintf("Error saving ledger to '%s': {{err}}", k.ledgerPath), err)
	}

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


示例18: NewLogger

func NewLogger(w io.Writer) (*Logger, error) {
	l := &Logger{}
	path, err := SystemTimeglassPathCreateIfNotExist()
	if err != nil {
		return nil, errwrap.Wrapf("Failed to find Timeglass system path: {{err}}", err)
	}

	l.path = filepath.Join(path, "daemon.log")
	l.file, err = os.OpenFile(l.path, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
	if err != nil {
		return nil, err
	}

	l.Writer = io.MultiWriter(l.file, w)
	return l, nil
}
开发者ID:rosetree,项目名称:glass,代码行数:16,代码来源:logger.go


示例19: ReadTimer

func (c *Client) ReadTimer(dir string) (*daemon.Timer, error) {
	timers := []*daemon.Timer{}
	params := url.Values{}
	params.Set("dir", dir)

	data, err := c.Call("timers.info", params)
	if err != nil {
		return nil, err
	}

	err = json.Unmarshal(data, &timers)
	if err != nil {
		return nil, errwrap.Wrapf(fmt.Sprintf("Failed to deserialize '%s' into a list of timers: {{err}}", data), err)
	}

	if len(timers) < 1 {
		return nil, fmt.Errorf("Expected at least one timer from the daemon")
	}

	return timers[0], nil
}
开发者ID:rosetree,项目名称:glass,代码行数:21,代码来源:client.go


示例20: timersDelete

func (s *Server) timersDelete(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 {
			err := s.keeper.Remove(dir)
			if err != nil {
				s.Respond(w, errwrap.Wrapf("Failed to remove timer: {{err}}", err))
				return
			}
		}
	}

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



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang registry.Key类代码示例发布时间:2022-05-28
下一篇:
Golang cli.App类代码示例发布时间:2022-05-28
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap