github.com/slspeek/camlistore_namedsearch@v0.0.0-20140519202248-ed6f70f7721a/cmd/camput/rawobj.go (about)

     1  /*
     2  Copyright 2011 Google Inc.
     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 main
    18  
    19  import (
    20  	"errors"
    21  	"flag"
    22  	"strings"
    23  
    24  	"camlistore.org/pkg/cmdmain"
    25  	"camlistore.org/pkg/schema"
    26  )
    27  
    28  type rawCmd struct {
    29  	vals   string // pipe separated key=value "camliVersion=1|camliType=foo", etc
    30  	signed bool
    31  }
    32  
    33  func init() {
    34  	cmdmain.RegisterCommand("rawobj", func(flags *flag.FlagSet) cmdmain.CommandRunner {
    35  		cmd := new(rawCmd)
    36  		flags.StringVar(&cmd.vals, "vals", "", "Pipe-separated key=value properties")
    37  		flags.BoolVar(&cmd.signed, "signed", true, "whether to sign the JSON object")
    38  		return cmd
    39  	})
    40  }
    41  
    42  func (c *rawCmd) Describe() string {
    43  	return "Upload a custom JSON schema blob."
    44  }
    45  
    46  func (c *rawCmd) Usage() {
    47  	cmdmain.Errorf("Usage: camput [globalopts] rawobj [rawopts]\n")
    48  }
    49  
    50  func (c *rawCmd) Examples() []string {
    51  	return []string{"(debug command)"}
    52  }
    53  
    54  func (c *rawCmd) RunCommand(args []string) error {
    55  	if len(args) > 0 {
    56  		return errors.New("Raw Object command doesn't take any additional arguments")
    57  	}
    58  
    59  	if c.vals == "" {
    60  		return errors.New("No values")
    61  	}
    62  
    63  	bb := schema.NewBuilder()
    64  	for _, kv := range strings.Split(c.vals, "|") {
    65  		kv := strings.SplitN(kv, "=", 2)
    66  		bb.SetRawStringField(kv[0], kv[1])
    67  	}
    68  
    69  	up := getUploader()
    70  	if c.signed {
    71  		put, err := up.UploadAndSignBlob(bb)
    72  		handleResult("raw-object-signed", put, err)
    73  		return err
    74  	}
    75  	cj, err := bb.JSON()
    76  	if err != nil {
    77  		return err
    78  	}
    79  	put, err := up.uploadString(cj)
    80  	handleResult("raw-object-unsigned", put, err)
    81  	return err
    82  }