github.com/olivere/camlistore@v0.0.0-20140121221811-1b7ac2da0199/cmd/camtool/index.go (about)

     1  /*
     2  Copyright 2013 The Camlistore Authors.
     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  	"flag"
    21  	"fmt"
    22  	"net/http"
    23  	"os"
    24  	"strconv"
    25  
    26  	"camlistore.org/pkg/client"
    27  	"camlistore.org/pkg/cmdmain"
    28  )
    29  
    30  type indexCmd struct {
    31  	verbose     bool
    32  	wipe        bool
    33  	insecureTLS bool
    34  }
    35  
    36  func init() {
    37  	cmdmain.RegisterCommand("index", func(flags *flag.FlagSet) cmdmain.CommandRunner {
    38  		cmd := new(indexCmd)
    39  		flags.BoolVar(&cmd.verbose, "verbose", false, "Be verbose.")
    40  		flags.BoolVar(&cmd.wipe, "wipe", false, "Erase and recreate all discovered indexes. NOOP for now.")
    41  		if debug, _ := strconv.ParseBool(os.Getenv("CAMLI_DEBUG")); debug {
    42  			flags.BoolVar(&cmd.insecureTLS, "insecure", false, "If set, when using TLS, the server's certificates verification is disabled, and they are not checked against the trustedCerts in the client configuration either.")
    43  		}
    44  		return cmd
    45  	})
    46  }
    47  
    48  func (c *indexCmd) Describe() string {
    49  	return "Synchronize blobs for all discovered blobs storage - indexer pairs."
    50  }
    51  
    52  func (c *indexCmd) Usage() {
    53  	fmt.Fprintf(os.Stderr, "Usage: camtool [globalopts] index [indexopts] \n")
    54  }
    55  
    56  func (c *indexCmd) RunCommand(args []string) error {
    57  	dc := c.discoClient()
    58  	syncHandlers, err := dc.SyncHandlers()
    59  	if err != nil {
    60  		return fmt.Errorf("sync handlers discovery failed: %v", err)
    61  	}
    62  
    63  	for _, sh := range syncHandlers {
    64  		if sh.ToIndex {
    65  			if err := c.sync(sh.From, sh.To); err != nil {
    66  				return fmt.Errorf("Error while indexing from %v to %v: %v", sh.From, sh.To, err)
    67  			}
    68  		}
    69  	}
    70  	return nil
    71  }
    72  
    73  func (c *indexCmd) sync(from, to string) error {
    74  	return (&syncCmd{
    75  		src:     from,
    76  		dest:    to,
    77  		verbose: c.verbose,
    78  		wipe:    c.wipe,
    79  	}).RunCommand(nil)
    80  }
    81  
    82  // discoClient returns a client initialized with a server
    83  // based from the configuration file. The returned client
    84  // can then be used to discover the blobRoot and syncHandlers.
    85  func (c *indexCmd) discoClient() *client.Client {
    86  	var cl *client.Client
    87  	cl = client.NewOrFail()
    88  	cl.InsecureTLS = c.insecureTLS
    89  	cl.SetHTTPClient(&http.Client{
    90  		Transport: cl.TransportForConfig(nil),
    91  	})
    92  	cl.SetupAuth()
    93  	return cl
    94  }