github.com/coreos/mantle@v0.13.0/cmd/ore/gcloud/index.go (about)

     1  // Copyright 2014 CoreOS, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package gcloud
    16  
    17  import (
    18  	"fmt"
    19  	"net/http"
    20  	"os"
    21  
    22  	"github.com/spf13/cobra"
    23  	"golang.org/x/net/context"
    24  
    25  	"github.com/coreos/mantle/auth"
    26  	"github.com/coreos/mantle/storage"
    27  	"github.com/coreos/mantle/storage/index"
    28  )
    29  
    30  var (
    31  	indexDryRun    bool
    32  	indexForce     bool
    33  	indexDelete    bool
    34  	indexDirs      bool
    35  	indexRecursive bool
    36  	indexTitle     string
    37  	cmdIndex       = &cobra.Command{
    38  		Use:   "index [options] gs://bucket/prefix/ [gs://...]",
    39  		Short: "Update HTML indexes",
    40  		Run:   runIndex,
    41  		Long: `Update HTML indexes for Google Storage.
    42  
    43  Scan a given Google Storage location and generate "index.html" under
    44  every directory prefix. If the --directories option is given then
    45  objects matching the directory prefixes are also created. For example,
    46  the pages generated for a bucket containing only "dir/obj":
    47  
    48      index.html     - a HTML index page listing dir
    49      dir/index.html - a HTML index page listing obj
    50      dir/           - an identical HTML index page
    51      dir            - a redirect page to dir/
    52  
    53  Do not enable --directories if you expect to be able to copy the tree to
    54  a local filesystem, the fake directories will conflict with the real ones!`,
    55  	}
    56  )
    57  
    58  func init() {
    59  	cmdIndex.Flags().BoolVarP(&indexDryRun,
    60  		"dry-run", "n", false,
    61  		"perform a trial run with no changes")
    62  	cmdIndex.Flags().BoolVarP(&indexForce,
    63  		"force", "f", false,
    64  		"overwrite objects even if they appear up to date")
    65  	cmdIndex.Flags().BoolVar(&indexDelete,
    66  		"delete", false, "delete index objects")
    67  	cmdIndex.Flags().BoolVarP(&indexRecursive, "recursive", "r", false,
    68  		"update nested prefixes")
    69  	cmdIndex.Flags().BoolVarP(&indexDirs,
    70  		"directories", "D", false,
    71  		"use objects to mimic a directory tree")
    72  	cmdIndex.Flags().StringVarP(&indexTitle, "html-title", "T", "",
    73  		"use the given title instead of bucket name in index pages")
    74  	GCloud.AddCommand(cmdIndex)
    75  }
    76  
    77  func runIndex(cmd *cobra.Command, args []string) {
    78  	if len(args) == 0 {
    79  		fmt.Fprintf(os.Stderr, "No URLs specified\n")
    80  		os.Exit(2)
    81  	}
    82  
    83  	ctx := context.Background()
    84  	client, err := auth.GoogleClient()
    85  	if err != nil {
    86  		fmt.Fprintf(os.Stderr, "Authentication failed: %v\n", err)
    87  		os.Exit(1)
    88  	}
    89  
    90  	for _, url := range args {
    91  		if err := updateTree(ctx, client, url); err != nil {
    92  			fmt.Fprintf(os.Stderr, "Failed: %v\n", err)
    93  			os.Exit(1)
    94  		}
    95  	}
    96  
    97  	if indexDryRun {
    98  		fmt.Printf("Dry-run successful!\n")
    99  	} else {
   100  		fmt.Printf("Update successful!\n")
   101  	}
   102  }
   103  
   104  func updateTree(ctx context.Context, client *http.Client, url string) error {
   105  	root, err := storage.NewBucket(client, url)
   106  	if err != nil {
   107  		return err
   108  	}
   109  	root.WriteDryRun(indexDryRun)
   110  	root.WriteAlways(indexForce)
   111  
   112  	if err = root.FetchPrefix(ctx, root.Prefix(), indexRecursive); err != nil {
   113  		return err
   114  	}
   115  
   116  	job := index.IndexJob{Bucket: root}
   117  	job.DirectoryHTML(indexDirs)
   118  	job.IndexHTML(true)
   119  	job.Delete(indexDelete)
   120  	job.Recursive(indexRecursive)
   121  	if indexTitle != "" {
   122  		job.Name(indexTitle)
   123  	}
   124  	return job.Do(ctx)
   125  }