~juju-qa/ubuntu/xenial/juju/2.0.2

« back to all changes in this revision

Viewing changes to src/github.com/vmware/govmomi/govc/vm/guest/upload.go

  • Committer: Nicholas Skaggs
  • Date: 2016-12-09 22:31:29 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161209223129-h2wbknpc8ty29zwp
ImportĀ upstreamĀ 2.0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
Copyright (c) 2014-2015 VMware, Inc. All Rights Reserved.
 
3
 
 
4
Licensed under the Apache License, Version 2.0 (the "License");
 
5
you may not use this file except in compliance with the License.
 
6
You may obtain a copy of the License at
 
7
 
 
8
    http://www.apache.org/licenses/LICENSE-2.0
 
9
 
 
10
Unless required by applicable law or agreed to in writing, software
 
11
distributed under the License is distributed on an "AS IS" BASIS,
 
12
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
13
See the License for the specific language governing permissions and
 
14
limitations under the License.
 
15
*/
 
16
 
 
17
package guest
 
18
 
 
19
import (
 
20
        "flag"
 
21
        "os"
 
22
 
 
23
        "github.com/vmware/govmomi/govc/cli"
 
24
        "golang.org/x/net/context"
 
25
)
 
26
 
 
27
type upload struct {
 
28
        *GuestFlag
 
29
        *FileAttrFlag
 
30
 
 
31
        overwrite bool
 
32
}
 
33
 
 
34
func init() {
 
35
        cli.Register("guest.upload", &upload{})
 
36
}
 
37
 
 
38
func (cmd *upload) Register(ctx context.Context, f *flag.FlagSet) {
 
39
        cmd.GuestFlag, ctx = newGuestFlag(ctx)
 
40
        cmd.GuestFlag.Register(ctx, f)
 
41
        cmd.FileAttrFlag, ctx = newFileAttrFlag(ctx)
 
42
        cmd.FileAttrFlag.Register(ctx, f)
 
43
 
 
44
        f.BoolVar(&cmd.overwrite, "f", false, "If set, the guest destination file is clobbered")
 
45
}
 
46
 
 
47
func (cmd *upload) Process(ctx context.Context) error {
 
48
        if err := cmd.GuestFlag.Process(ctx); err != nil {
 
49
                return err
 
50
        }
 
51
        if err := cmd.FileAttrFlag.Process(ctx); err != nil {
 
52
                return err
 
53
        }
 
54
        return nil
 
55
}
 
56
 
 
57
func (cmd *upload) Run(ctx context.Context, f *flag.FlagSet) error {
 
58
        m, err := cmd.FileManager()
 
59
        if err != nil {
 
60
                return err
 
61
        }
 
62
 
 
63
        src := f.Arg(0)
 
64
        dst := f.Arg(1)
 
65
 
 
66
        s, err := os.Stat(src)
 
67
        if err != nil {
 
68
                return err
 
69
        }
 
70
 
 
71
        url, err := m.InitiateFileTransferToGuest(context.TODO(), cmd.Auth(), dst, cmd.Attr(), s.Size(), cmd.overwrite)
 
72
        if err != nil {
 
73
                return err
 
74
        }
 
75
 
 
76
        u, err := cmd.ParseURL(url)
 
77
        if err != nil {
 
78
                return err
 
79
        }
 
80
 
 
81
        c, err := cmd.Client()
 
82
        if err != nil {
 
83
                return nil
 
84
        }
 
85
 
 
86
        return c.Client.UploadFile(src, u, nil)
 
87
}