From 2588ff8c83f9da43e431bb50a7205e42c7056d64 Mon Sep 17 00:00:00 2001 From: mpl Date: Sun, 29 Dec 2013 00:03:58 +0100 Subject: [PATCH] camtool: apparently forgot to add index.go in 2d85e017ff53b64c4cc5569a2da93df6d2ed9c1f Change-Id: I6b7ce9e70b15d622935950ac8b1f06d843590659 --- cmd/camtool/index.go | 84 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 cmd/camtool/index.go diff --git a/cmd/camtool/index.go b/cmd/camtool/index.go new file mode 100644 index 000000000..9dc2b0249 --- /dev/null +++ b/cmd/camtool/index.go @@ -0,0 +1,84 @@ +/* +Copyright 2013 The Camlistore Authors. + +Licensed 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 main + +import ( + "flag" + "fmt" + "os" + + "camlistore.org/pkg/client" + "camlistore.org/pkg/cmdmain" +) + +type indexCmd struct { + verbose bool + wipe bool +} + +func init() { + cmdmain.RegisterCommand("index", func(flags *flag.FlagSet) cmdmain.CommandRunner { + cmd := new(indexCmd) + flags.BoolVar(&cmd.verbose, "verbose", false, "Be verbose.") + flags.BoolVar(&cmd.wipe, "wipe", false, "Erase and recreate all discovered indexes. NOOP for now.") + return cmd + }) +} + +func (c *indexCmd) Describe() string { + return "Synchronize blobs for all discovered blobs storage - indexer pairs." +} + +func (c *indexCmd) Usage() { + fmt.Fprintf(os.Stderr, "Usage: camtool [globalopts] index [indexopts] \n") +} + +func (c *indexCmd) RunCommand(args []string) error { + dc := c.discoClient() + syncHandlers, err := dc.SyncHandlers() + if err != nil { + return fmt.Errorf("sync handlers discovery failed: %v", err) + } + + for _, sh := range syncHandlers { + if sh.ToIndex { + if err := c.sync(sh.From, sh.To); err != nil { + return fmt.Errorf("Error while indexing from %v to %v: %v", sh.From, sh.To, err) + } + } + } + return nil +} + +func (c *indexCmd) sync(from, to string) error { + return (&syncCmd{ + src: from, + dest: to, + verbose: c.verbose, + wipe: c.wipe, + }).RunCommand(nil) +} + +// discoClient returns a client initialized with a server +// based from the configuration file. The returned client +// can then be used to discover the blobRoot and syncHandlers. +func (c *indexCmd) discoClient() *client.Client { + var cl *client.Client + cl = client.NewOrFail() + cl.SetupAuth() + return cl +}