blob: ebed8959753ca3b975bb8f835f6d6143b039842e [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package utils
import (
"encoding/json"
"fmt"
"net/url"
"os/user"
"bufio"
"github.com/hokaccha/go-prettyjson"
"github.com/openwhisk/openwhisk-client-go/whisk"
"os"
"reflect"
"strings"
)
// ActionRecord is a container to keep track of
// a whisk action struct and a location filepath we use to
// map files and manifest declared actions
type ActionRecord struct {
Action *whisk.Action
Packagename string
Filepath string
}
type TriggerRecord struct {
Trigger *whisk.Trigger
Packagename string
}
type RuleRecord struct {
Rule *whisk.Rule
Packagename string
}
// Utility to convert hostname to URL object
func GetURLBase(host string) (*url.URL, error) {
urlBase := fmt.Sprintf("%s/api", host)
url, err := url.Parse(urlBase)
if len(url.Scheme) == 0 || len(url.Host) == 0 {
urlBase = fmt.Sprintf("https://%s/api", host)
url, err = url.Parse(urlBase)
}
return url, err
}
func GetHomeDirectory() string {
usr, err := user.Current()
Check(err)
return usr.HomeDir
}
// Potentially complex structures(such as DeploymentApplication, DeploymentPackage)
// could implement those interface which is convenient for put, get subtract in
// containers etc.
type Comparable interface {
HashCode() uint32
Equals() bool
}
func IsFeedAction(trigger *whisk.Trigger) (string, bool) {
for _, annotation := range trigger.Annotations {
if annotation.Key == "feed" {
return annotation.Value.(string), true
}
}
return "", false
}
func IsJSON(s string) (interface{}, bool) {
var js interface{}
if json.Unmarshal([]byte(s), &js) == nil {
return js, true
}
return nil, false
}
func PrettyJSON(j interface{}) string {
formatter := prettyjson.NewFormatter()
bytes, err := formatter.Marshal(j)
Check(err)
return string(bytes)
}
// Common utilities
// Prompt for user input
func Ask(reader *bufio.Reader, question string, def string) string {
fmt.Print(question + " (" + def + "): ")
answer, _ := reader.ReadString('\n')
len := len(answer)
if len == 1 {
return def
}
return answer[:len-1]
}
// Get the env variable value by key.
// Get the env variable if the key is start by $
func GetEnvVar(key interface{}) interface{} {
if reflect.TypeOf(key).String() == "string" {
if strings.HasPrefix(key.(string), "$") {
envkey := strings.Split(key.(string), "$")[1]
value := os.Getenv(envkey)
if value != "" {
return value
}
return envkey
}
return key.(string)
}
return key
}
var kindToJSON []string = []string{"", "boolean", "integer", "integer", "integer", "integer", "integer", "integer", "integer", "integer",
"integer", "integer", "integer", "number", "number", "number", "number", "array", "", "", "", "object", "", "", "string", "", ""}
// Gets JSON type name
func GetJSONType(j interface{}) string {
fmt.Print(reflect.TypeOf(j).Kind())
return kindToJSON[reflect.TypeOf(j).Kind()]
}