github.com/vmware/govmomi@v0.43.0/govc/vm/guest/chown.go (about)

     1  /*
     2  Copyright (c) 2017 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  	"context"
    21  	"flag"
    22  	"strconv"
    23  	"strings"
    24  
    25  	"github.com/vmware/govmomi/govc/cli"
    26  	"github.com/vmware/govmomi/vim25/types"
    27  )
    28  
    29  type chown struct {
    30  	*GuestFlag
    31  }
    32  
    33  func init() {
    34  	cli.Register("guest.chown", &chown{})
    35  }
    36  
    37  func (cmd *chown) Register(ctx context.Context, f *flag.FlagSet) {
    38  	cmd.GuestFlag, ctx = newGuestFlag(ctx)
    39  	cmd.GuestFlag.Register(ctx, f)
    40  }
    41  
    42  func (cmd *chown) Process(ctx context.Context) error {
    43  	if err := cmd.GuestFlag.Process(ctx); err != nil {
    44  		return err
    45  	}
    46  	return nil
    47  }
    48  
    49  func (cmd *chown) Usage() string {
    50  	return "UID[:GID] FILE"
    51  }
    52  
    53  func (cmd *chown) Description() string {
    54  	return `Change FILE UID and GID on VM.
    55  
    56  Examples:
    57    govc guest.chown -vm $name UID[:GID] /var/log/foo.log`
    58  }
    59  
    60  func (cmd *chown) Run(ctx context.Context, f *flag.FlagSet) error {
    61  	if f.NArg() != 2 {
    62  		return flag.ErrHelp
    63  	}
    64  
    65  	m, err := cmd.FileManager()
    66  	if err != nil {
    67  		return err
    68  	}
    69  
    70  	var attr types.GuestPosixFileAttributes
    71  
    72  	ids := strings.SplitN(f.Arg(0), ":", 2)
    73  	if len(ids) == 0 {
    74  		return flag.ErrHelp
    75  	}
    76  
    77  	id, err := strconv.Atoi(ids[0])
    78  	if err != nil {
    79  		return err
    80  	}
    81  
    82  	attr.OwnerId = new(int32)
    83  	*attr.OwnerId = int32(id)
    84  
    85  	if len(ids) == 2 {
    86  		id, err = strconv.Atoi(ids[1])
    87  		if err != nil {
    88  			return err
    89  		}
    90  
    91  		attr.GroupId = new(int32)
    92  		*attr.GroupId = int32(id)
    93  	}
    94  
    95  	return m.ChangeFileAttributes(ctx, cmd.Auth(), f.Arg(1), &attr)
    96  }