func parseSIProgress(pb progress.Meter, stdout io.Reader) error {
	if pb == nil {
		pb = &progress.NullProgress{}
	}

	scanner := bufio.NewScanner(stdout)
	// s-i is funny, total changes during the runs
	total := 0.0
	pb.Start("ubuntu-core", 100)

	for scanner.Scan() {
		if os.Getenv("SNAPPY_DEBUG") != "" {
			fmt.Println(scanner.Text())
		}

		jsonStream := strings.NewReader(scanner.Text())
		dec := json.NewDecoder(jsonStream)
		var genericData genericJSON
		if err := dec.Decode(&genericData); err != nil {
			// we ignore invalid json here and continue
			// the parsing if s-i-cli or ubuntu-core-upgrader
			// output something unexpected (like stray debug
			// output or whatnot)
			continue
		}

		switch {
		case genericData.Type == "spinner":
			pb.Spin(genericData.Message)
		case genericData.Type == "error":
			return fmt.Errorf("error from %s: %s", systemImageCli, genericData.Message)
		case genericData.Type == "progress":
			if total != genericData.Total {
				total = genericData.Total
				pb.SetTotal(total)
			}
			pb.Set(genericData.Now)
		}
	}
	// ugly: avoid Spin() artifacts
	pb.Notify("\nApply done")

	if err := scanner.Err(); err != nil {
		return err
	}

	return nil
}
Beispiel #2
0
func (f *fakeSnappyBackend) Download(name, channel string, checker func(*snap.Info) error, p progress.Meter, auther store.Authenticator) (*snap.Info, string, error) {
	p.Notify("download")
	var macaroon string
	if auther != nil {
		macaroon = auther.(*auth.MacaroonAuthenticator).Macaroon
	}
	f.ops = append(f.ops, fakeOp{
		op:       "download",
		macaroon: macaroon,
		name:     name,
		channel:  channel,
	})
	p.SetTotal(float64(f.fakeTotalProgress))
	p.Set(float64(f.fakeCurrentProgress))

	revno := 11
	if channel == "channel-for-7" {
		revno = 7
	}

	info := &snap.Info{
		SideInfo: snap.SideInfo{
			OfficialName: strings.Split(name, ".")[0],
			Channel:      channel,
			SnapID:       "snapIDsnapidsnapidsnapidsnapidsn",
			Revision:     revno,
		},
		Version: name,
	}

	err := checker(info)
	if err != nil {
		return nil, "", err
	}

	return info, "downloaded-snap-path", nil
}