本文整理匯總了Golang中github.com/flynn/flynn/Godeps/_workspace/src/github.com/cheggaaa/pb.New函數的典型用法代碼示例。如果您正苦於以下問題:Golang New函數的具體用法?Golang New怎麽用?Golang New使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了New函數的11個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: runPgRestore
func runPgRestore(args *docopt.Args, client *controller.Client, config *runConfig) error {
config.Stdin = os.Stdin
var size int64
if filename := args.String["--file"]; filename != "" {
f, err := os.Open(filename)
if err != nil {
return err
}
defer f.Close()
stat, err := f.Stat()
if err != nil {
return err
}
size = stat.Size()
config.Stdin = f
}
if !args.Bool["--quiet"] && term.IsTerminal(os.Stderr.Fd()) {
bar := pb.New(0)
bar.SetUnits(pb.U_BYTES)
if size > 0 {
bar.Total = size
} else {
bar.ShowBar = false
}
bar.ShowSpeed = true
bar.Output = os.Stderr
bar.Start()
defer bar.Finish()
config.Stdin = bar.NewProxyReader(config.Stdin)
}
return pgRestore(client, config)
}
示例2: runRedisDump
func runRedisDump(args *docopt.Args, client controller.Client, config *runConfig) error {
config.Stdout = os.Stdout
if filename := args.String["--file"]; filename != "" {
f, err := os.Create(filename)
if err != nil {
return err
}
defer f.Close()
config.Stdout = f
}
if !args.Bool["--quiet"] && term.IsTerminal(os.Stderr.Fd()) {
bar := pb.New(0)
bar.SetUnits(pb.U_BYTES)
bar.ShowBar = false
bar.ShowSpeed = true
bar.Output = os.Stderr
bar.Start()
defer bar.Finish()
config.Stdout = io.MultiWriter(config.Stdout, bar)
}
config.Entrypoint = []string{"/bin/dump-flynn-redis"}
return runJob(client, *config)
}
示例3: Upload
func (g *Gist) Upload(log log15.Logger) error {
if len(g.Files) == 0 {
return errors.New("cannot create empty gist")
}
payload, err := json.Marshal(g)
if err != nil {
log.Error("error preparing gist content", "err", err)
return err
}
var body io.Reader = bytes.NewReader(payload)
if term.IsTerminal(os.Stderr.Fd()) {
bar := pb.New(len(payload))
bar.SetUnits(pb.U_BYTES)
bar.ShowSpeed = true
bar.Output = os.Stderr
bar.Start()
defer bar.Finish()
body = bar.NewProxyReader(body)
}
req, err := http.NewRequest("POST", "https://api.github.com/gists", body)
if err != nil {
log.Error("error preparing HTTP request", "err", err)
return err
}
req.Header.Set("Content-Type", "application/json")
log.Info("creating anonymous gist")
res, err := http.DefaultClient.Do(req)
if err != nil {
log.Error("error uploading gist content", "err", err)
return err
}
defer res.Body.Close()
if res.StatusCode != http.StatusCreated {
e := fmt.Sprintf("unexpected HTTP status: %d", res.StatusCode)
log.Error(e)
return errors.New(e)
}
if err := json.NewDecoder(res.Body).Decode(g); err != nil {
log.Error("error decoding HTTP response", "err", err)
return err
}
return nil
}
示例4: runClusterBackup
func runClusterBackup(args *docopt.Args) error {
client, err := getClusterClient()
if err != nil {
return err
}
var bar *pb.ProgressBar
if term.IsTerminal(os.Stderr.Fd()) {
bar = pb.New(0)
bar.SetUnits(pb.U_BYTES)
bar.ShowBar = false
bar.ShowSpeed = true
bar.Output = os.Stderr
bar.Start()
}
var dest io.Writer = os.Stdout
if filename := args.String["--file"]; filename != "" {
f, err := os.Create(filename)
if err != nil {
return err
}
defer f.Close()
dest = f
}
fmt.Fprintln(os.Stderr, "Creating cluster backup...")
if bar != nil {
dest = io.MultiWriter(dest, bar)
}
if err := backup.Run(client, dest); err != nil {
return err
}
if bar != nil {
bar.Finish()
}
fmt.Fprintln(os.Stderr, "Backup complete.")
return nil
}
示例5: main
func main() {
count := 5000
bar := pb.New(count)
// show percents (by default already true)
bar.ShowPercent = true
// show bar (by default already true)
bar.ShowPercent = true
// no need counters
bar.ShowCounters = true
bar.ShowTimeLeft = true
// and start
bar.Start()
for i := 0; i < count; i++ {
bar.Increment()
time.Sleep(time.Millisecond)
}
bar.FinishPrint("The End!")
}
示例6: runExport
func runExport(args *docopt.Args, client *controller.Client) error {
var dest io.Writer = os.Stdout
if filename := args.String["--file"]; filename != "" {
f, err := os.Create(filename)
if err != nil {
return fmt.Errorf("error creating export file: %s", err)
}
defer f.Close()
dest = f
}
app, err := client.GetApp(mustApp())
if err != nil {
return fmt.Errorf("error getting app: %s", err)
}
tw := backup.NewTarWriter(app.Name, dest)
defer tw.Close()
if err := tw.WriteJSON("app.json", app); err != nil {
return fmt.Errorf("error exporting app: %s", err)
}
routes, err := client.RouteList(mustApp())
if err != nil {
return fmt.Errorf("error getting routes: %s", err)
}
if err := tw.WriteJSON("routes.json", routes); err != nil {
return fmt.Errorf("error exporting routes: %s", err)
}
release, err := client.GetAppRelease(mustApp())
if err != nil && err != controller.ErrNotFound {
return fmt.Errorf("error retrieving app: %s", err)
} else if err == nil {
// Do not allow the exporting of passwords.
delete(release.Env, "REDIS_PASSWORD")
if err := tw.WriteJSON("release.json", release); err != nil {
return fmt.Errorf("error exporting release: %s", err)
}
}
artifact, err := client.GetArtifact(release.ArtifactID)
if err != nil && err != controller.ErrNotFound {
return fmt.Errorf("error retrieving artifact: %s", err)
} else if err == nil {
if err := tw.WriteJSON("artifact.json", artifact); err != nil {
return fmt.Errorf("error exporting artifact: %s", err)
}
}
formation, err := client.GetFormation(mustApp(), release.ID)
if err != nil && err != controller.ErrNotFound {
return fmt.Errorf("error retrieving formation: %s", err)
} else if err == nil {
if err := tw.WriteJSON("formation.json", formation); err != nil {
return fmt.Errorf("error exporting formation: %s", err)
}
}
var bar *pb.ProgressBar
if !args.Bool["--quiet"] && term.IsTerminal(os.Stderr.Fd()) {
bar = pb.New(0)
bar.SetUnits(pb.U_BYTES)
bar.ShowBar = false
bar.ShowSpeed = true
bar.Output = os.Stderr
bar.Start()
defer bar.Finish()
}
if slug, ok := release.Env["SLUG_URL"]; ok {
reqR, reqW := io.Pipe()
config := runConfig{
App: mustApp(),
Release: release.ID,
DisableLog: true,
Entrypoint: []string{"curl"},
Args: []string{"--include", "--raw", slug},
Stdout: reqW,
Stderr: ioutil.Discard,
}
if bar != nil {
config.Stdout = io.MultiWriter(config.Stdout, bar)
}
go func() {
if err := runJob(client, config); err != nil {
shutdown.Fatalf("error retrieving slug: %s", err)
}
}()
res, err := http.ReadResponse(bufio.NewReader(reqR), nil)
if err != nil {
return fmt.Errorf("error reading slug response: %s", err)
}
if res.StatusCode != 200 {
return fmt.Errorf("unexpected status getting slug: %d", res.StatusCode)
}
length, err := strconv.Atoi(res.Header.Get("Content-Length"))
if err != nil {
//.........這裏部分代碼省略.........
示例7: runImport
func runImport(args *docopt.Args, client *controller.Client) error {
var src io.Reader = os.Stdin
if filename := args.String["--file"]; filename != "" {
f, err := os.Open(filename)
if err != nil {
return fmt.Errorf("error opening export file: %s", err)
}
defer f.Close()
src = f
}
tr := tar.NewReader(src)
var (
app *ct.App
release *ct.Release
artifact *ct.Artifact
formation *ct.Formation
routes []router.Route
slug io.Reader
pgDump io.Reader
uploadSize int64
)
numResources := 0
numRoutes := 1
for {
header, err := tr.Next()
if err == io.EOF {
break
} else if err != nil {
return fmt.Errorf("error reading export tar: %s", err)
}
switch path.Base(header.Name) {
case "app.json":
app = &ct.App{}
if err := json.NewDecoder(tr).Decode(app); err != nil {
return fmt.Errorf("error decoding app: %s", err)
}
app.ID = ""
case "release.json":
release = &ct.Release{}
if err := json.NewDecoder(tr).Decode(release); err != nil {
return fmt.Errorf("error decoding release: %s", err)
}
release.ID = ""
release.ArtifactID = ""
case "artifact.json":
artifact = &ct.Artifact{}
if err := json.NewDecoder(tr).Decode(artifact); err != nil {
return fmt.Errorf("error decoding artifact: %s", err)
}
artifact.ID = ""
case "formation.json":
formation = &ct.Formation{}
if err := json.NewDecoder(tr).Decode(formation); err != nil {
return fmt.Errorf("error decoding formation: %s", err)
}
formation.AppID = ""
formation.ReleaseID = ""
case "routes.json":
if err := json.NewDecoder(tr).Decode(&routes); err != nil {
return fmt.Errorf("error decoding routes: %s", err)
}
for _, route := range routes {
route.ID = ""
route.ParentRef = ""
}
case "slug.tar.gz":
f, err := ioutil.TempFile("", "slug.tar.gz")
if err != nil {
return fmt.Errorf("error creating slug tempfile: %s", err)
}
defer f.Close()
defer os.Remove(f.Name())
if _, err := io.Copy(f, tr); err != nil {
return fmt.Errorf("error reading slug: %s", err)
}
if _, err := f.Seek(0, os.SEEK_SET); err != nil {
return fmt.Errorf("error seeking slug tempfile: %s", err)
}
slug = f
uploadSize += header.Size
case "postgres.dump":
f, err := ioutil.TempFile("", "postgres.dump")
if err != nil {
return fmt.Errorf("error creating db tempfile: %s", err)
}
defer f.Close()
defer os.Remove(f.Name())
if _, err := io.Copy(f, tr); err != nil {
return fmt.Errorf("error reading db dump: %s", err)
}
if _, err := f.Seek(0, os.SEEK_SET); err != nil {
return fmt.Errorf("error seeking db tempfile: %s", err)
}
pgDump = f
uploadSize += header.Size
}
}
//.........這裏部分代碼省略.........
示例8: runExport
func runExport(args *docopt.Args, client *controller.Client) error {
var dest io.Writer = os.Stdout
if filename := args.String["--file"]; filename != "" {
f, err := os.Create(filename)
if err != nil {
return fmt.Errorf("error creating export file: %s", err)
}
defer f.Close()
dest = f
}
tw := tar.NewWriter(dest)
defer tw.Close()
uid := syscall.Getuid()
header := func(name string, length int) error {
return tw.WriteHeader(&tar.Header{
Name: path.Join(mustApp(), name),
Mode: 0644,
Size: int64(length),
ModTime: time.Now(),
Typeflag: tar.TypeReg,
Uid: uid,
Gid: uid,
})
}
writeJSON := func(name string, v interface{}) error {
data, err := json.MarshalIndent(v, "", " ")
if err != nil {
return err
}
if err := header(name, len(data)+1); err != nil {
return err
}
if _, err := tw.Write(data); err != nil {
return err
}
if _, err := tw.Write([]byte("\n")); err != nil {
return err
}
return nil
}
app, err := client.GetApp(mustApp())
if err != nil {
return fmt.Errorf("error getting app: %s", err)
}
if err := writeJSON("app.json", app); err != nil {
return fmt.Errorf("error exporting app: %s", err)
}
release, err := client.GetAppRelease(mustApp())
if err != nil && err != controller.ErrNotFound {
return fmt.Errorf("error retrieving app: %s", err)
} else if err == nil {
if err := writeJSON("release.json", release); err != nil {
return fmt.Errorf("error exporting release: %s", err)
}
}
artifact, err := client.GetArtifact(release.ArtifactID)
if err != nil && err != controller.ErrNotFound {
return fmt.Errorf("error retrieving artifact: %s", err)
} else if err == nil {
if err := writeJSON("artifact.json", artifact); err != nil {
return fmt.Errorf("error exporting artifact: %s", err)
}
}
formation, err := client.GetFormation(mustApp(), release.ID)
if err != nil && err != controller.ErrNotFound {
return fmt.Errorf("error retrieving formation: %s", err)
} else if err == nil {
if err := writeJSON("formation.json", formation); err != nil {
return fmt.Errorf("error exporting formation: %s", err)
}
}
var bar *pb.ProgressBar
if !args.Bool["--quiet"] && term.IsTerminal(os.Stderr.Fd()) {
bar = pb.New(0)
bar.SetUnits(pb.U_BYTES)
bar.ShowBar = false
bar.ShowSpeed = true
bar.Output = os.Stderr
bar.Start()
defer bar.Finish()
}
if slug, ok := release.Env["SLUG_URL"]; ok {
reqR, reqW := io.Pipe()
config := runConfig{
App: mustApp(),
Release: release.ID,
DisableLog: true,
Entrypoint: []string{"curl"},
Args: []string{"--include", "--raw", slug},
Stdout: reqW,
Stderr: ioutil.Discard,
}
if bar != nil {
//.........這裏部分代碼省略.........
示例9: runClusterBackup
func runClusterBackup(args *docopt.Args) error {
client, err := getClusterClient()
if err != nil {
return err
}
var bar *pb.ProgressBar
if term.IsTerminal(os.Stderr.Fd()) {
bar = pb.New(0)
bar.SetUnits(pb.U_BYTES)
bar.ShowBar = false
bar.ShowSpeed = true
bar.Output = os.Stderr
bar.Start()
}
var dest io.Writer = os.Stdout
if filename := args.String["--file"]; filename != "" {
f, err := os.Create(filename)
if err != nil {
return err
}
defer f.Close()
dest = f
}
fmt.Fprintln(os.Stderr, "Creating cluster backup...")
tw := NewTarWriter("flynn-backup-"+time.Now().UTC().Format("2006-01-02_150405"), dest)
defer tw.Close()
// get app and release details for key apps
data := make(map[string]*ct.ExpandedFormation, 4)
for _, name := range []string{"postgres", "discoverd", "flannel", "controller"} {
app, err := client.GetApp(name)
if err != nil {
return fmt.Errorf("error getting %s app details: %s", name, err)
}
release, err := client.GetAppRelease(app.ID)
if err != nil {
return fmt.Errorf("error getting %s app release: %s", name, err)
}
formation, err := client.GetFormation(app.ID, release.ID)
if err != nil {
return fmt.Errorf("error getting %s app formation: %s", name, err)
}
artifact, err := client.GetArtifact(release.ArtifactID)
if err != nil {
return fmt.Errorf("error getting %s app artifact: %s", name, err)
}
data[name] = &ct.ExpandedFormation{
App: app,
Release: release,
Artifact: artifact,
Processes: formation.Processes,
}
}
if err := tw.WriteJSON("flynn.json", data); err != nil {
return err
}
config := &runConfig{
App: "postgres",
Release: data["postgres"].Release.ID,
Entrypoint: []string{"sh"},
Args: []string{"-c", "pg_dumpall --clean --if-exists | gzip -9"},
Env: map[string]string{
"PGHOST": "leader.postgres.discoverd",
"PGUSER": "flynn",
"PGPASSWORD": data["postgres"].Release.Env["PGPASSWORD"],
},
DisableLog: true,
}
if err := tw.WriteCommandOutput(client, "postgres.sql.gz", config, bar); err != nil {
return fmt.Errorf("error dumping database: %s", err)
}
if bar != nil {
bar.Finish()
}
fmt.Fprintln(os.Stderr, "Backup complete.")
return nil
}
示例10: runExport
func runExport(args *docopt.Args, client controller.Client) error {
var dest io.Writer = os.Stdout
if filename := args.String["--file"]; filename != "" {
f, err := os.Create(filename)
if err != nil {
return fmt.Errorf("error creating export file: %s", err)
}
defer f.Close()
dest = f
}
app, err := client.GetApp(mustApp())
if err != nil {
return fmt.Errorf("error getting app: %s", err)
}
var bar backup.ProgressBar
if !args.Bool["--quiet"] && term.IsTerminal(os.Stderr.Fd()) {
b := pb.New(0)
b.SetUnits(pb.U_BYTES)
b.ShowBar = false
b.ShowSpeed = true
b.Output = os.Stderr
b.Start()
defer b.Finish()
bar = b
}
tw := backup.NewTarWriter(app.Name, dest, bar)
defer tw.Close()
if err := tw.WriteJSON("app.json", app); err != nil {
return fmt.Errorf("error exporting app: %s", err)
}
routes, err := client.RouteList(mustApp())
if err != nil {
return fmt.Errorf("error getting routes: %s", err)
}
if err := tw.WriteJSON("routes.json", routes); err != nil {
return fmt.Errorf("error exporting routes: %s", err)
}
release, err := client.GetAppRelease(mustApp())
if err == controller.ErrNotFound {
// if the app has no release then there is nothing more to export
return nil
} else if err != nil {
return fmt.Errorf("error retrieving app: %s", err)
} else if err == nil {
// Do not allow the exporting of passwords.
delete(release.Env, "REDIS_PASSWORD")
if err := tw.WriteJSON("release.json", release); err != nil {
return fmt.Errorf("error exporting release: %s", err)
}
}
if artifactID := release.ImageArtifactID(); artifactID != "" {
artifact, err := client.GetArtifact(artifactID)
if err != nil && err != controller.ErrNotFound {
return fmt.Errorf("error retrieving artifact: %s", err)
} else if err == nil {
if err := tw.WriteJSON("artifact.json", artifact); err != nil {
return fmt.Errorf("error exporting artifact: %s", err)
}
}
}
formation, err := client.GetFormation(mustApp(), release.ID)
if err != nil && err != controller.ErrNotFound {
return fmt.Errorf("error retrieving formation: %s", err)
} else if err == nil {
if err := tw.WriteJSON("formation.json", formation); err != nil {
return fmt.Errorf("error exporting formation: %s", err)
}
}
// expect releases deployed via git to have a slug as their first file
// artifact, and legacy releases to have SLUG_URL set
var slugURL string
if release.IsGitDeploy() && len(release.FileArtifactIDs()) > 0 {
slugArtifact, err := client.GetArtifact(release.FileArtifactIDs()[0])
if err != nil && err != controller.ErrNotFound {
return fmt.Errorf("error retrieving slug artifact: %s", err)
} else if err == nil {
slugURL = slugArtifact.URI
}
} else if u, ok := release.Env["SLUG_URL"]; ok {
slugURL = u
}
if slugURL != "" {
reqR, reqW := io.Pipe()
config := runConfig{
App: mustApp(),
Release: release.ID,
DisableLog: true,
Entrypoint: []string{"curl"},
Args: []string{"--include", "--raw", slugURL},
Stdout: reqW,
//.........這裏部分代碼省略.........
示例11: main
func main() {
// check args
if len(os.Args) < 3 {
printUsage()
return
}
sourceName, destName := os.Args[1], os.Args[2]
// check source
var source io.Reader
var sourceSize int64
if strings.HasPrefix(sourceName, "http://") {
// open as url
resp, err := http.Get(sourceName)
if err != nil {
fmt.Printf("Can't get %s: %v\n", sourceName, err)
return
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
fmt.Printf("Server return non-200 status: %v\n", resp.Status)
return
}
i, _ := strconv.Atoi(resp.Header.Get("Content-Length"))
sourceSize = int64(i)
source = resp.Body
} else {
// open as file
s, err := os.Open(sourceName)
if err != nil {
fmt.Printf("Can't open %s: %v\n", sourceName, err)
return
}
defer s.Close()
// get source size
sourceStat, err := s.Stat()
if err != nil {
fmt.Printf("Can't stat %s: %v\n", sourceName, err)
return
}
sourceSize = sourceStat.Size()
source = s
}
// create dest
dest, err := os.Create(destName)
if err != nil {
fmt.Printf("Can't create %s: %v\n", destName, err)
return
}
defer dest.Close()
// create bar
bar := pb.New(int(sourceSize)).SetUnits(pb.U_BYTES).SetRefreshRate(time.Millisecond * 10)
bar.ShowSpeed = true
bar.Start()
// create multi writer
writer := io.MultiWriter(dest, bar)
// and copy
io.Copy(writer, source)
bar.Finish()
}