github.com/jancarloviray/community@v0.41.1-0.20170124221257-33a66c87cf2f/sdk/documize/main.go (about)

     1  // Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
     2  //
     3  // This software (Documize Community Edition) is licensed under
     4  // GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
     5  //
     6  // You can operate outside the AGPL restrictions by purchasing
     7  // Documize Enterprise Edition and obtaining a commercial license
     8  // by contacting <sales@documize.com>.
     9  //
    10  // https://documize.com
    11  
    12  package main
    13  
    14  import (
    15  	"flag"
    16  	"fmt"
    17  	"os"
    18  
    19  	sdk "github.com/documize/community/sdk"
    20  )
    21  
    22  func main() {
    23  
    24  	flagSet := flag.NewFlagSet("documize client flags", flag.ExitOnError)
    25  
    26  	url, auth, folder, action := flagSet.String("api", os.Getenv("DOCUMIZEAPI"),
    27  		"the url of the endpoint (defaults to environment variable DOCUMIZEAPI)"), //e.g. http://localhost:5002
    28  		flagSet.String("auth", os.Getenv("DOCUMIZEAUTH"), //e.g. demo1:mick@jagger.com:demo123
    29  			"the authorization credentials in the form domain:email:password (defaults to the environment variable DOCUMIZEAUTH)"),
    30  		flagSet.String("folder", "", "the Documize folder to use"),
    31  		flagSet.String("action", "load", "the Documize action to take")
    32  
    33  	if err := flagSet.Parse(os.Args[1:]); err != nil {
    34  		fmt.Println("unable to parse Documize arguments:", err)
    35  		os.Exit(1)
    36  	}
    37  
    38  	if *url == "" {
    39  		fmt.Println("Please set the environment variable DOCUMIZEAPI or use the -api flag")
    40  		os.Exit(1)
    41  	}
    42  
    43  	if *auth == "" {
    44  		fmt.Println("Please set the environment variable DOCUMIZEAUTH or use the -auth flag")
    45  		os.Exit(1)
    46  	}
    47  
    48  	c, e := sdk.NewClient(*url, *auth)
    49  	if e != nil {
    50  		fmt.Println("unable to create Documize SDK client for", *auth, "Error:", e)
    51  		os.Exit(1)
    52  	}
    53  
    54  	switch *action {
    55  	case "load":
    56  		folderID := checkFolder(c, folder)
    57  		for _, arg := range flagSet.Args() {
    58  			_, ce := c.LoadFile(folderID, arg)
    59  			if ce == nil {
    60  				fmt.Println("Loaded file " + arg + " into Documize folder " + *folder)
    61  			} else {
    62  				fmt.Println("Failed to load file " + arg + " into Documize folder " + *folder + " Error: " + ce.Error())
    63  			}
    64  		}
    65  	}
    66  }
    67  
    68  func checkFolder(c *sdk.Client, folder *string) string {
    69  	if *folder == "" {
    70  		*folder = os.Getenv("DOCUMIZEFOLDER")
    71  		if *folder == "" {
    72  			fmt.Println("Please set the environment variable DOCUMIZEFOLDER or use the -folder flag")
    73  			os.Exit(1)
    74  		}
    75  	}
    76  	fids, err := c.GetNamedFolderIDs(*folder)
    77  	if err != nil {
    78  		fmt.Println("Error reading folder IDs: " + err.Error())
    79  		os.Exit(1)
    80  	}
    81  	if len(fids) != 1 {
    82  		fmt.Println("There is no single folder called: " + *folder)
    83  		os.Exit(1)
    84  	}
    85  	return fids[0]
    86  }