github.com/slspeek/camlistore_namedsearch@v0.0.0-20140519202248-ed6f70f7721a/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  	"os"
    23  	"strconv"
    24  
    25  	"camlistore.org/pkg/client"
    26  	"camlistore.org/pkg/cmdmain"
    27  )
    28  
    29  type indexCmd struct {
    30  	verbose     bool
    31  	wipe        bool
    32  	insecureTLS bool
    33  }
    34  
    35  func init() {
    36  	cmdmain.RegisterCommand("index", func(flags *flag.FlagSet) cmdmain.CommandRunner {
    37  		cmd := new(indexCmd)
    38  		flags.BoolVar(&cmd.verbose, "verbose", false, "Be verbose.")
    39  		flags.BoolVar(&cmd.wipe, "wipe", false, "Erase and recreate all discovered indexes. NOOP for now.")
    40  		if debug, _ := strconv.ParseBool(os.Getenv("CAMLI_DEBUG")); debug {
    41  			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.")
    42  		}
    43  		return cmd
    44  	})
    45  }
    46  
    47  func (c *indexCmd) Describe() string {
    48  	return "Synchronize blobs for all discovered blobs storage - indexer pairs."
    49  }
    50  
    51  func (c *indexCmd) Usage() {
    52  	fmt.Fprintf(os.Stderr, "Usage: camtool [globalopts] index [indexopts] \n")
    53  }
    54  
    55  func (c *indexCmd) RunCommand(args []string) error {
    56  	dc := c.discoClient()
    57  	syncHandlers, err := dc.SyncHandlers()
    58  	if err != nil {
    59  		return fmt.Errorf("sync handlers discovery failed: %v", err)
    60  	}
    61  
    62  	for _, sh := range syncHandlers {
    63  		if sh.ToIndex {
    64  			if err := c.sync(sh.From, sh.To); err != nil {
    65  				return fmt.Errorf("Error while indexing from %v to %v: %v", sh.From, sh.To, err)
    66  			}
    67  		}
    68  	}
    69  	return nil
    70  }
    71  
    72  func (c *indexCmd) sync(from, to string) error {
    73  	return (&syncCmd{
    74  		src:     from,
    75  		dest:    to,
    76  		verbose: c.verbose,
    77  		wipe:    c.wipe,
    78  	}).RunCommand(nil)
    79  }
    80  
    81  // discoClient returns a client initialized with a server
    82  // based from the configuration file. The returned client
    83  // can then be used to discover the blobRoot and syncHandlers.
    84  func (c *indexCmd) discoClient() *client.Client {
    85  	return newClient("")
    86  }