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 (
18	"github.com/skratchdot/open-golang/open"
19)
20
21type OpenType uint
22
23const (
24	OpenNone OpenType = 1 << iota
25	FileManagerOpen
26	BrowserOpen
27	IdOpen
28)
29
30type opener func(string) error
31
32func (g *Commands) Open(ot OpenType) error {
33	byId := (ot & IdOpen) != 0
34	kvChan := g.urler(byId, g.opts.Sources)
35
36	for kv := range kvChan {
37		switch kv.value.(type) {
38		case error:
39			g.log.LogErrf("%s: %s\n", kv.key, kv.value)
40			continue
41		}
42
43		openArgs := []string{}
44		canAddUrl := (ot & BrowserOpen) != 0
45
46		if byId {
47			canAddUrl = true
48		} else if (ot & FileManagerOpen) != 0 {
49			openArgs = append(openArgs, g.context.AbsPathOf(kv.key))
50		}
51
52		if canAddUrl {
53			if castKey, ok := kv.value.(string); ok {
54				openArgs = append(openArgs, castKey)
55			}
56		}
57
58		for _, arg := range openArgs {
59			if err := open.Start(arg); err != nil {
60				g.log.LogErrf("err: %v %q\n", err, arg)
61			}
62		}
63	}
64
65	return nil
66}
67