1// Copyright 2015 Google Inc. All Rights Reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//      http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package drive
16
17import "fmt"
18
19func (g *Commands) Star(byId bool) error {
20	return starring(g, true, byId)
21}
22
23func (g *Commands) UnStar(byId bool) error {
24	return starring(g, false, byId)
25}
26
27func starring(g *Commands, starred, byId bool) (composedErr error) {
28	kvChan := resolver(g, byId, g.opts.Sources, noopOnFile)
29
30	verb := "Starred"
31	if !starred {
32		verb = "Unstarred"
33	}
34
35	for kv := range kvChan {
36		file, ok := kv.value.(*File)
37		if !ok {
38			g.log.LogErrf("%s: %s\n", kv.key, kv.value)
39			continue
40		}
41
42		if file == nil {
43			continue
44		}
45
46		updatedFile, err := g.rem.updateStarred(file.Id, starred)
47		if err != nil {
48			composedErr = reComposeError(composedErr, fmt.Sprintf("%q %v", kv.key, err))
49		} else if updatedFile != nil {
50			name := fmt.Sprintf("%q", kv.key)
51			if kv.key != updatedFile.Id {
52				name = fmt.Sprintf("%s aka %q", name, updatedFile.Id)
53			}
54			g.log.LogErrf("%s %s\n", verb, name)
55		}
56	}
57
58	return composedErr
59}
60