<commit_before>package OffenderUtil

import (
	"encoding/json"
	"fmt"
	"net/http"
	"time"
)

// Offenders represents a response from FamilyWatchdog
type Offenders struct {
	Searchtype string     `json:"searchtype"`
	Vers       string     `json:"vers"`
	Status     string     `json:"status"`
	Offenders  []Offender `json:"offenders"`
}

// Offender is a single offender
type Offender struct {
	OffenderID string `json:"offenderid"`
	Photo      string `json:"photo"`
	Name       string `json:"name"`
	FirstName  string `json:"firstname"`
	MiddleName string `json:"middlename"`
	LastName   string `json:"lastname"`
	Suffix     string `json:"suffix"`
	Dob        string `json:"dob"`
	Age        string `json:"age"`
	Sex        string `json:"sex"`
	Race       string `json:"race"`
	Hair       string `json:"hair"`
	Eye        string `json:"eye"`
	Height     string `json:"height"`
	Weight     string `json:"weight"`
	Street1    string `json:"street1"`
	Street2    string `json:"street2"`
	City       string `json:"city"`
	State      string `json:"state"`
	Zipcode    string `json:"zipcode"`
	County     string `json:"county"`
	MatchType  string `json:"matchtype"`
	Latitude   string `json:"latitude"`
	Longitude  string `json:"longitude"`
}

var myClient = &http.Client{Timeout: 10 * time.Second}

var apiKey string

// SetAPIKey will set the api key
func SetAPIKey(a string) {
	apiKey = a
}

// getJSON will make a request and return the response as a struct supplied
func getJSON(params string, target interface{}) error {
	url := fmt.Sprintf("http://services.familywatchdog.us/rest/json.asp?key=%s&lite=0%s", apiKey, params)

	r, err := myClient.Get(url)

	if err != nil {
		return err
	}

	defer r.Body.Close()

	return json.NewDecoder(r.Body).Decode(target)
}

// SearchByZip returns sex offenders near zip
func SearchByZip(zip string) Offenders {
	params := fmt.Sprintf("&type=searchbyzipcode&zipcode=%s", zip)
	records := Offenders{}

	// getJSON will return the offenders JSON
	getJSON(params, &records)

	return Offenders(records)
}

// SearchByNameState returns sex offenders with name in state
func SearchByNameState(fname string, lname string, state string) Offenders {
	params := fmt.Sprintf("&type=searchbynamestate&fname=%s&lname=%s&state=%s", fname, lname, state)
	records := Offenders{}

	// getJSON will return the offenders JSON
	getJSON(params, &records)

	return Offenders(records)
}

// SearchByLatLong returns sex offenders with name in state
func SearchByLatLong(minLat string, maxLat string, minLong string, maxLong string) Offenders {
	params := fmt.Sprintf("&type=searchbylatlong&minlat=%s&maxlat=%s&minlong=%s&maxlong=%s", minLat, maxLat, minLong, maxLong)
	records := Offenders{}

	// getJSON will return the offenders JSON
	getJSON(params, &records)

	return Offenders(records)
}
<commit_msg>added new structs<commit_after>package OffenderUtil

import (
	"encoding/json"
	"fmt"
	"log"
	"net/http"
	"time"
)

// Offenders represents a response from FamilyWatchdog
type Offenders struct {
	Searchtype string     `json:"searchtype"`
	Vers       string     `json:"vers"`
	Status     string     `json:"status"`
	Offenders  []Offender `json:"offenders"`
}

// Aliases is offender aliases
type Aliases struct {
	Name       string `json:"name"`
	FirstName  string `json:"firstname"`
	MiddleName string `json:"middlename"`
	LastName   string `json:"lastname"`
	Suffix     string `json:"suffix"`
}

// OtherAddresses are additional addresses
type OtherAddresses struct {
	Type      string `json:"type"`
	Name      string `json:"name"`
	Street1   string `json:"street1"`
	Street2   string `json:"street2"`
	City      string `json:"city"`
	State     string `json:"state"`
	Zipcode   string `json:"zipcode"`
	MatchType string `json:"matchtype"`
	Latitude  string `json:"latitude"`
	Longitude string `json:"longitude"`
}

// Convictions are the charges
type Convictions struct {
	Charge          string `json:"charge"`
	Statute         string `json:"statute"`
	VictimAge       string `json:"victimage"`
	VictimSex       string `json:"victimsex"`
	ConvictionState string `json:"convictionstate"`
}

// Markings are additional markings
type Markings struct {
	Description string `json:"description"`
}

// Birthdates are unknown
type Birthdates struct {
	Dob string `json:"dob"`
}

// PossibleNicknames are nicknames
type PossibleNicknames struct {
	FirstName string `json:"firstname"`
}

// Offender is a single offender
type Offender struct {
	OffenderID        string              `json:"offenderid"`
	Photo             string              `json:"photo"`
	Name              string              `json:"name"`
	FirstName         string              `json:"firstname"`
	MiddleName        string              `json:"middlename"`
	LastName          string              `json:"lastname"`
	Suffix            string              `json:"suffix"`
	Dob               string              `json:"dob"`
	Age               string              `json:"age"`
	Sex               string              `json:"sex"`
	Race              string              `json:"race"`
	Hair              string              `json:"hair"`
	Eye               string              `json:"eye"`
	Height            string              `json:"height"`
	Weight            string              `json:"weight"`
	Street1           string              `json:"street1"`
	Street2           string              `json:"street2"`
	City              string              `json:"city"`
	State             string              `json:"state"`
	Zipcode           string              `json:"zipcode"`
	County            string              `json:"county"`
	MatchType         string              `json:"matchtype"`
	Latitude          string              `json:"latitude"`
	Longitude         string              `json:"longitude"`
	Aliases           []Aliases           `json:"aliases"`
	OtherAddresses    []OtherAddresses    `json:"otheraddresses"`
	Convictions       []Convictions       `json:"convictions"`
	Markings          []Markings          `json:"markings"`
	Birthdates        []Birthdates        `json:"birthdates"`
	PossibleNicknames []PossibleNicknames `json:"possiblenicknames"`
}

var myClient = &http.Client{Timeout: 10 * time.Second}

var apiKey string

// SetAPIKey will set the api key
func SetAPIKey(a string) {
	apiKey = a
}

// getJSON will make a request and return the response as a struct supplied
func getJSON(params string, target interface{}) error {
	url := fmt.Sprintf("http://services.familywatchdog.us/rest/json.asp?key=%s&lite=0%s", apiKey, params)

	r, err := myClient.Get(url)

	if err != nil {
		return err
	}

	defer r.Body.Close()

	return json.NewDecoder(r.Body).Decode(target)
}

// SearchByZip returns sex offenders near zip
func SearchByZip(zip string) Offenders {
	params := fmt.Sprintf("&type=searchbyzipcode&zipcode=%s", zip)
	records := Offenders{}

	// getJSON will return the offenders JSON
	err := getJSON(params, &records)
	log.Println(err)

	return Offenders(records)
}

// SearchByNameState returns sex offenders with name in state
func SearchByNameState(fname string, lname string, state string) Offenders {
	params := fmt.Sprintf("&type=searchbynamestate&fname=%s&lname=%s&state=%s", fname, lname, state)
	records := Offenders{}

	// getJSON will return the offenders JSON
	getJSON(params, &records)

	return Offenders(records)
}

// SearchByLatLong returns sex offenders with name in state
func SearchByLatLong(minLat string, maxLat string, minLong string, maxLong string) Offenders {
	params := fmt.Sprintf("&type=searchbylatlong&minlat=%s&maxlat=%s&minlong=%s&maxlong=%s", minLat, maxLat, minLong, maxLong)
	records := Offenders{}

	// getJSON will return the offenders JSON
	getJSON(params, &records)

	return Offenders(records)
}
<|endoftext|>


<commit_before>// Copyright 2016, Cossack Labs Limited
//
// 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"
	"github.com/cossacklabs/acra/cmd"
	"github.com/cossacklabs/acra/keystore"
	"github.com/cossacklabs/acra/utils"
	"github.com/cossacklabs/themis/gothemis/keys"
	"os"
)

var DEFAULT_CONFIG_PATH = utils.GetConfigPathByName("acra_genkeys")

func create_keys(filename, output_dir string) {
	keypair, err := keys.New(keys.KEYTYPE_EC)
	if err != nil {
		panic(err)
	}
	if output_dir[len(output_dir)-1] != '/' {
		output_dir = output_dir + "/"
	}
	file, err := os.OpenFile(fmt.Sprintf("%v%v", output_dir, filename), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
	if err != nil {
		panic(err)
	}

	n, err := file.Write(keypair.Private.Value)
	if n != len(keypair.Private.Value) {
		panic("Error in writing private key")
	}
	if err != nil {
		panic(err)
	}
	fmt.Println(file.Name())

	file, err = os.OpenFile(fmt.Sprintf("%v%v.pub", output_dir, filename), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
	if err != nil {
		panic(err)
	}

	n, err = file.Write(keypair.Public.Value)
	if n != len(keypair.Public.Value) {
		panic("Error in writing public key")
	}
	if err != nil {
		panic(err)
	}
	fmt.Println(file.Name())
}

func main() {
	client_id := flag.String("client_id", "client", "Client id")
	acraproxy := flag.Bool("acraproxy", false, "Create keypair only for acraproxy")
	acraserver := flag.Bool("acraserver", false, "Create keypair only for acraserver")
	data_keys := flag.Bool("storage", false, "Create keypair for data encryption/decryption")
	output_dir := flag.String("output", keystore.DEFAULT_KEY_DIR_SHORT, "Folder where will be saved keys")

	err := cmd.Parse(DEFAULT_CONFIG_PATH)
	if err != nil {
		fmt.Printf("Error: %v\n", utils.ErrorMessage("Can't parse args", err))
		os.Exit(1)
	}

	store, err := keystore.NewFilesystemKeyStore(*output_dir)
	if err != nil {
		panic(err)
	}

	if *acraproxy {
		err = store.GenerateProxyKeys([]byte(*client_id))
		if err != nil {
			panic(err)
		}
	} else if *acraserver {
		err = store.GenerateServerKeys([]byte(*client_id))
		if err != nil {
			panic(err)
		}
	} else if *data_keys {
		err = store.GenerateDataEncryptionKeys([]byte(*client_id))
		if err != nil {
			panic(err)
		}
	} else {
		err = store.GenerateProxyKeys([]byte(*client_id))
		if err != nil {
			panic(err)
		}

		err = store.GenerateServerKeys([]byte(*client_id))
		if err != nil {
			panic(err)
		}

		err = store.GenerateDataEncryptionKeys([]byte(*client_id))
		if err != nil {
			panic(err)
		}
	}
}
<commit_msg>delete redundant code<commit_after>// Copyright 2016, Cossack Labs Limited
//
// 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"
	"github.com/cossacklabs/acra/cmd"
	"github.com/cossacklabs/acra/keystore"
	"github.com/cossacklabs/acra/utils"
	"github.com/cossacklabs/themis/gothemis/keys"
	"os"
)

var DEFAULT_CONFIG_PATH = utils.GetConfigPathByName("acra_genkeys")

func main() {
	client_id := flag.String("client_id", "client", "Client id")
	acraproxy := flag.Bool("acraproxy", false, "Create keypair only for acraproxy")
	acraserver := flag.Bool("acraserver", false, "Create keypair only for acraserver")
	data_keys := flag.Bool("storage", false, "Create keypair for data encryption/decryption")
	output_dir := flag.String("output", keystore.DEFAULT_KEY_DIR_SHORT, "Folder where will be saved keys")

	err := cmd.Parse(DEFAULT_CONFIG_PATH)
	if err != nil {
		fmt.Printf("Error: %v\n", utils.ErrorMessage("Can't parse args", err))
		os.Exit(1)
	}

	store, err := keystore.NewFilesystemKeyStore(*output_dir)
	if err != nil {
		panic(err)
	}

	if *acraproxy {
		err = store.GenerateProxyKeys([]byte(*client_id))
		if err != nil {
			panic(err)
		}
	} else if *acraserver {
		err = store.GenerateServerKeys([]byte(*client_id))
		if err != nil {
			panic(err)
		}
	} else if *data_keys {
		err = store.GenerateDataEncryptionKeys([]byte(*client_id))
		if err != nil {
			panic(err)
		}
	} else {
		err = store.GenerateProxyKeys([]byte(*client_id))
		if err != nil {
			panic(err)
		}

		err = store.GenerateServerKeys([]byte(*client_id))
		if err != nil {
			panic(err)
		}

		err = store.GenerateDataEncryptionKeys([]byte(*client_id))
		if err != nil {
			panic(err)
		}
	}
}
<|endoftext|>


<commit_before>package main_test

import (
	"net/http/httptest"
	"os"
	"time"

	"code.cloudfoundry.org/auction/simulation/simulationrep"
	"code.cloudfoundry.org/bbs/models"
	"code.cloudfoundry.org/locket"
	"code.cloudfoundry.org/rep"

	"code.cloudfoundry.org/lager"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"

	executorfakes "code.cloudfoundry.org/executor/fakes"
	"code.cloudfoundry.org/rep/evacuation/evacuation_context/fake_evacuation_context"
	rephandlers "code.cloudfoundry.org/rep/handlers"
	"code.cloudfoundry.org/rep/maintain"
	"github.com/tedsuo/ifrit"
	"github.com/tedsuo/rata"
)

type FakeCell struct {
	cellID      string
	repUrl      string
	stack       string
	server      *httptest.Server
	heartbeater ifrit.Process
	logger      lager.Logger

	SimulationRep rep.SimClient
}

func SpinUpFakeCell(cellPresenceClient maintain.CellPresenceClient, cellID string, repUrl string, stack string) *FakeCell {
	fakeRep := &FakeCell{
		cellID: cellID,
		repUrl: repUrl,
		stack:  stack,
		logger: lager.NewLogger("fake-cell"),
	}

	fakeRep.SpinUp(cellPresenceClient)
	Eventually(func() bool {
		cells, err := cellPresenceClient.Cells(logger)
		Expect(err).NotTo(HaveOccurred())
		return cells.HasCellID(cellID)
	}).Should(BeTrue())

	return fakeRep
}

func (f *FakeCell) LRPs() ([]rep.LRP, error) {
	state, err := f.SimulationRep.State(logger)
	if err != nil {
		return nil, err
	}
	return state.LRPs, nil
}

func (f *FakeCell) Tasks() ([]rep.Task, error) {
	state, err := f.SimulationRep.State(logger)
	if err != nil {
		return nil, err
	}
	return state.Tasks, nil
}

func (f *FakeCell) SpinUp(cellPresenceClient maintain.CellPresenceClient) {
	//make a test-friendly AuctionRepDelegate using the auction package's SimulationRepDelegate
	f.SimulationRep = simulationrep.New(f.stack, "Z0", rep.Resources{
		DiskMB:     100,
		MemoryMB:   100,
		Containers: 100,
	}, []string{"my-driver"})

	//spin up an http auction server
	logger := lager.NewLogger(f.cellID)
	logger.RegisterSink(lager.NewWriterSink(GinkgoWriter, lager.INFO))

	fakeExecutorClient := new(executorfakes.FakeClient)
	fakeEvacuatable := new(fake_evacuation_context.FakeEvacuatable)

	handlers := rephandlers.NewLegacy(f.SimulationRep, fakeExecutorClient, fakeEvacuatable, logger)
	router, err := rata.NewRouter(rep.Routes, handlers)
	Expect(err).NotTo(HaveOccurred())
	f.server = httptest.NewServer(router)

	presence := models.NewCellPresence(
		f.cellID,
		f.server.URL,
		f.repUrl,
		"az1",
		models.NewCellCapacity(512, 1024, 124),
		[]string{},
		[]string{},
		[]string{},
		[]string{},
	)

	f.heartbeater = ifrit.Invoke(cellPresenceClient.NewCellPresenceRunner(logger, &presence, time.Second, locket.DefaultSessionTTL))
}

func (f *FakeCell) Stop() {
	f.server.Close()
	f.heartbeater.Signal(os.Interrupt)
	Eventually(f.heartbeater.Wait()).Should(Receive())
}
<commit_msg>handle new `auctioncellrep.AuctionCellClient` interface<commit_after>package main_test

import (
	"net/http/httptest"
	"os"
	"time"

	"code.cloudfoundry.org/auction/simulation/simulationrep"
	"code.cloudfoundry.org/bbs/models"
	"code.cloudfoundry.org/locket"
	"code.cloudfoundry.org/rep"

	"code.cloudfoundry.org/lager"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"

	executorfakes "code.cloudfoundry.org/executor/fakes"
	"code.cloudfoundry.org/rep/auctioncellrep/auctioncellrepfakes"
	"code.cloudfoundry.org/rep/evacuation/evacuation_context/fake_evacuation_context"
	rephandlers "code.cloudfoundry.org/rep/handlers"
	"code.cloudfoundry.org/rep/maintain"
	"github.com/tedsuo/ifrit"
	"github.com/tedsuo/rata"
)

type FakeCell struct {
	cellID      string
	repUrl      string
	stack       string
	server      *httptest.Server
	heartbeater ifrit.Process
	logger      lager.Logger

	SimulationRep rep.SimClient
}

func SpinUpFakeCell(cellPresenceClient maintain.CellPresenceClient, cellID string, repUrl string, stack string) *FakeCell {
	fakeRep := &FakeCell{
		cellID: cellID,
		repUrl: repUrl,
		stack:  stack,
		logger: lager.NewLogger("fake-cell"),
	}

	fakeRep.SpinUp(cellPresenceClient)
	Eventually(func() bool {
		cells, err := cellPresenceClient.Cells(logger)
		Expect(err).NotTo(HaveOccurred())
		return cells.HasCellID(cellID)
	}).Should(BeTrue())

	return fakeRep
}

func (f *FakeCell) LRPs() ([]rep.LRP, error) {
	state, err := f.SimulationRep.State(logger)
	if err != nil {
		return nil, err
	}
	return state.LRPs, nil
}

func (f *FakeCell) Tasks() ([]rep.Task, error) {
	state, err := f.SimulationRep.State(logger)
	if err != nil {
		return nil, err
	}
	return state.Tasks, nil
}

func (f *FakeCell) SpinUp(cellPresenceClient maintain.CellPresenceClient) {
	//make a test-friendly AuctionRepDelegate using the auction package's SimulationRepDelegate
	f.SimulationRep = simulationrep.New(f.stack, "Z0", rep.Resources{
		DiskMB:     100,
		MemoryMB:   100,
		Containers: 100,
	}, []string{"my-driver"})

	//spin up an http auction server
	logger := lager.NewLogger(f.cellID)
	logger.RegisterSink(lager.NewWriterSink(GinkgoWriter, lager.INFO))

	fakeExecutorClient := new(executorfakes.FakeClient)
	fakeEvacuatable := new(fake_evacuation_context.FakeEvacuatable)

	fakeAuctionCellClient := new(auctioncellrepfakes.FakeAuctionCellClient)
	fakeAuctionCellClient.StateStub = func(logger lager.Logger) (rep.CellState, bool, error) {
		state, err := f.SimulationRep.State(logger)
		if err != nil {
			return rep.CellState{}, false, err
		}
		return state, true, nil
	}
	fakeAuctionCellClient.PerformStub = f.SimulationRep.Perform

	handlers := rephandlers.NewLegacy(fakeAuctionCellClient, fakeExecutorClient, fakeEvacuatable, logger)
	router, err := rata.NewRouter(rep.Routes, handlers)
	Expect(err).NotTo(HaveOccurred())
	f.server = httptest.NewServer(router)

	presence := models.NewCellPresence(
		f.cellID,
		f.server.URL,
		f.repUrl,
		"az1",
		models.NewCellCapacity(512, 1024, 124),
		[]string{},
		[]string{},
		[]string{},
		[]string{},
	)

	f.heartbeater = ifrit.Invoke(cellPresenceClient.NewCellPresenceRunner(logger, &presence, time.Second, locket.DefaultSessionTTL))
}

func (f *FakeCell) Stop() {
	f.server.Close()
	f.heartbeater.Signal(os.Interrupt)
	Eventually(f.heartbeater.Wait()).Should(Receive())
}
<|endoftext|>


<commit_before>// Copyright 2019 OpenTelemetry 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 (
	"go.opentelemetry.io/collector/component"
	"go.opentelemetry.io/collector/component/componenterror"
	"go.opentelemetry.io/collector/service/defaultcomponents"

	"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/alibabacloudlogserviceexporter"
	"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/awsemfexporter"
	"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/awsxrayexporter"
	"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/azuremonitorexporter"
	"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/carbonexporter"
	"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/elasticexporter"
	"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/honeycombexporter"
	"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/jaegerthrifthttpexporter"
	"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/kinesisexporter"
	"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/newrelicexporter"
	"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/sapmexporter"
	"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/sentryexporter"
	"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/signalfxexporter"
	"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/splunkhecexporter"
	"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/stackdriverexporter"
	"github.com/open-telemetry/opentelemetry-collector-contrib/extension/httpforwarder"
	"github.com/open-telemetry/opentelemetry-collector-contrib/extension/observer/hostobserver"
	"github.com/open-telemetry/opentelemetry-collector-contrib/extension/observer/k8sobserver"
	"github.com/open-telemetry/opentelemetry-collector-contrib/processor/k8sprocessor"
	"github.com/open-telemetry/opentelemetry-collector-contrib/processor/metricstransformprocessor"
	"github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor"
	"github.com/open-telemetry/opentelemetry-collector-contrib/processor/routingprocessor"
	"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/awsecscontainermetricsreceiver"
	"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/awsxrayreceiver"
	"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/carbonreceiver"
	"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/collectdreceiver"
	"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/dockerstatsreceiver"
	"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/k8sclusterreceiver"
	"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/kubeletstatsreceiver"
	"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/prometheusexecreceiver"
	"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/receivercreator"
	"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/redisreceiver"
	"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/sapmreceiver"
	"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/signalfxreceiver"
	"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/simpleprometheusreceiver"
	"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/splunkhecreceiver"
	"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/statsdreceiver"
	"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/wavefrontreceiver"
)

func components() (component.Factories, error) {
	var errs []error
	factories, err := defaultcomponents.Components()
	if err != nil {
		return component.Factories{}, err
	}

	extensions := []component.ExtensionFactory{
		k8sobserver.NewFactory(),
		hostobserver.NewFactory(),
		httpforwarder.NewFactory(),
	}

	for _, ext := range factories.Extensions {
		extensions = append(extensions, ext)
	}

	factories.Extensions, err = component.MakeExtensionFactoryMap(extensions...)
	if err != nil {
		errs = append(errs, err)
	}

	receivers := []component.ReceiverFactory{
		collectdreceiver.NewFactory(),
		sapmreceiver.NewFactory(),
		signalfxreceiver.NewFactory(),
		carbonreceiver.NewFactory(),
		wavefrontreceiver.NewFactory(),
		redisreceiver.NewFactory(),
		kubeletstatsreceiver.NewFactory(),
		simpleprometheusreceiver.NewFactory(),
		k8sclusterreceiver.NewFactory(),
		prometheusexecreceiver.NewFactory(),
		receivercreator.NewFactory(),
		statsdreceiver.NewFactory(),
		awsxrayreceiver.NewFactory(),
		splunkhecreceiver.NewFactory(),
		dockerstatsreceiver.NewFactory(),
		awsecscontainermetricsreceiver.NewFactory(),
	}
	for _, rcv := range factories.Receivers {
		receivers = append(receivers, rcv)
	}
	factories.Receivers, err = component.MakeReceiverFactoryMap(receivers...)
	if err != nil {
		errs = append(errs, err)
	}

	exporters := []component.ExporterFactory{
		stackdriverexporter.NewFactory(),
		azuremonitorexporter.NewFactory(),
		signalfxexporter.NewFactory(),
		sapmexporter.NewFactory(),
		kinesisexporter.NewFactory(),
		awsxrayexporter.NewFactory(),
		carbonexporter.NewFactory(),
		honeycombexporter.NewFactory(),
		jaegerthrifthttpexporter.NewFactory(),
		newrelicexporter.NewFactory(),
		splunkhecexporter.NewFactory(),
		elasticexporter.NewFactory(),
		alibabacloudlogserviceexporter.NewFactory(),
		awsemfexporter.NewFactory(),
		sentryexporter.NewFactory(),
	}
	for _, exp := range factories.Exporters {
		exporters = append(exporters, exp)
	}
	factories.Exporters, err = component.MakeExporterFactoryMap(exporters...)
	if err != nil {
		errs = append(errs, err)
	}

	processors := []component.ProcessorFactory{
		k8sprocessor.NewFactory(),
		resourcedetectionprocessor.NewFactory(),
		metricstransformprocessor.NewFactory(),
		routingprocessor.NewFactory(),
	}
	for _, pr := range factories.Processors {
		processors = append(processors, pr)
	}
	factories.Processors, err = component.MakeProcessorFactoryMap(processors...)
	if err != nil {
		errs = append(errs, err)
	}

	return factories, componenterror.CombineErrors(errs)
}
<commit_msg>Sorted the extensions/receivers/processors/exporters in components.go (#1230)<commit_after>// Copyright 2019 OpenTelemetry 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 (
	"go.opentelemetry.io/collector/component"
	"go.opentelemetry.io/collector/component/componenterror"
	"go.opentelemetry.io/collector/service/defaultcomponents"

	"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/alibabacloudlogserviceexporter"
	"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/awsemfexporter"
	"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/awsxrayexporter"
	"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/azuremonitorexporter"
	"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/carbonexporter"
	"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/elasticexporter"
	"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/honeycombexporter"
	"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/jaegerthrifthttpexporter"
	"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/kinesisexporter"
	"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/newrelicexporter"
	"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/sapmexporter"
	"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/sentryexporter"
	"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/signalfxexporter"
	"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/splunkhecexporter"
	"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/stackdriverexporter"
	"github.com/open-telemetry/opentelemetry-collector-contrib/extension/httpforwarder"
	"github.com/open-telemetry/opentelemetry-collector-contrib/extension/observer/hostobserver"
	"github.com/open-telemetry/opentelemetry-collector-contrib/extension/observer/k8sobserver"
	"github.com/open-telemetry/opentelemetry-collector-contrib/processor/k8sprocessor"
	"github.com/open-telemetry/opentelemetry-collector-contrib/processor/metricstransformprocessor"
	"github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor"
	"github.com/open-telemetry/opentelemetry-collector-contrib/processor/routingprocessor"
	"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/awsecscontainermetricsreceiver"
	"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/awsxrayreceiver"
	"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/carbonreceiver"
	"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/collectdreceiver"
	"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/dockerstatsreceiver"
	"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/k8sclusterreceiver"
	"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/kubeletstatsreceiver"
	"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/prometheusexecreceiver"
	"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/receivercreator"
	"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/redisreceiver"
	"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/sapmreceiver"
	"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/signalfxreceiver"
	"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/simpleprometheusreceiver"
	"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/splunkhecreceiver"
	"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/statsdreceiver"
	"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/wavefrontreceiver"
)

func components() (component.Factories, error) {
	var errs []error
	factories, err := defaultcomponents.Components()
	if err != nil {
		return component.Factories{}, err
	}

	extensions := []component.ExtensionFactory{
		hostobserver.NewFactory(),
		httpforwarder.NewFactory(),
		k8sobserver.NewFactory(),
	}

	for _, ext := range factories.Extensions {
		extensions = append(extensions, ext)
	}

	factories.Extensions, err = component.MakeExtensionFactoryMap(extensions...)
	if err != nil {
		errs = append(errs, err)
	}

	receivers := []component.ReceiverFactory{
		awsecscontainermetricsreceiver.NewFactory(),
		awsxrayreceiver.NewFactory(),
		carbonreceiver.NewFactory(),
		collectdreceiver.NewFactory(),
		dockerstatsreceiver.NewFactory(),
		k8sclusterreceiver.NewFactory(),
		kubeletstatsreceiver.NewFactory(),
		prometheusexecreceiver.NewFactory(),
		receivercreator.NewFactory(),
		redisreceiver.NewFactory(),
		sapmreceiver.NewFactory(),
		signalfxreceiver.NewFactory(),
		simpleprometheusreceiver.NewFactory(),
		splunkhecreceiver.NewFactory(),
		statsdreceiver.NewFactory(),
		wavefrontreceiver.NewFactory(),
	}
	for _, rcv := range factories.Receivers {
		receivers = append(receivers, rcv)
	}
	factories.Receivers, err = component.MakeReceiverFactoryMap(receivers...)
	if err != nil {
		errs = append(errs, err)
	}

	exporters := []component.ExporterFactory{
		alibabacloudlogserviceexporter.NewFactory(),
		awsemfexporter.NewFactory(),
		awsxrayexporter.NewFactory(),
		azuremonitorexporter.NewFactory(),
		carbonexporter.NewFactory(),
		elasticexporter.NewFactory(),
		honeycombexporter.NewFactory(),
		jaegerthrifthttpexporter.NewFactory(),
		kinesisexporter.NewFactory(),
		newrelicexporter.NewFactory(),
		sapmexporter.NewFactory(),
		sentryexporter.NewFactory(),
		signalfxexporter.NewFactory(),
		splunkhecexporter.NewFactory(),
		stackdriverexporter.NewFactory(),
	}
	for _, exp := range factories.Exporters {
		exporters = append(exporters, exp)
	}
	factories.Exporters, err = component.MakeExporterFactoryMap(exporters...)
	if err != nil {
		errs = append(errs, err)
	}

	processors := []component.ProcessorFactory{
		k8sprocessor.NewFactory(),
		metricstransformprocessor.NewFactory(),
		resourcedetectionprocessor.NewFactory(),
		routingprocessor.NewFactory(),
	}
	for _, pr := range factories.Processors {
		processors = append(processors, pr)
	}
	factories.Processors, err = component.MakeProcessorFactoryMap(processors...)
	if err != nil {
		errs = append(errs, err)
	}

	return factories, componenterror.CombineErrors(errs)
}
<|endoftext|>


<commit_before>package main

import (
    "github.com/qmsk/clusterf/config"
    "github.com/qmsk/clusterf/docker"
    "fmt"
)

// Translate a docker container to a service config
func configContainer (container *docker.Container) (configs []config.Config) {
    configBackend := config.ConfigServiceBackend{}

    if serviceLabel, set := container.Labels["net.qmsk.clusterf.service"]; !set {
        return
    } else {
        configBackend.ServiceName = serviceLabel
    }

    configBackend.BackendName = container.ID
    configBackend.Backend.IPv4 = container.IPv4.String()

    for _, port := range container.Ports {
        // select correct port by label
        portLabel := container.Labels[fmt.Sprintf("net.qmsk.clusterf.backend.%s", port.Proto)]

        if portLabel != "" && portLabel != fmt.Sprintf("%v", port.Port) {
            continue
        }

        switch port.Proto {
        case "tcp":
            configBackend.Backend.TCP = port.Port
        case "udp":
            configBackend.Backend.UDP = port.Port
        }
    }

    configs = append(configs, configBackend)

    return
}
<commit_msg>cluster-docker: new labeling scheme for multiple services<commit_after>package main

import (
    "github.com/qmsk/clusterf/config"
    "github.com/qmsk/clusterf/docker"
    "fmt"
    "strings"
    "log"
)

// Translate a docker container to a service config
func configContainer (container *docker.Container) (configs []config.Config) {
    // map ports
    containerPorts := make(map[string]docker.Port)

    for _, port := range container.Ports {
        containerPorts[fmt.Sprintf("%s:%d", port.Proto, port.Port)] = port
    }

    // services
    for _, serviceName := range strings.Fields(container.Labels["net.qmsk.clusterf.service"]) {
        configBackend := config.ConfigServiceBackend{
            ServiceName: serviceName,
            BackendName: container.ID,
        }

        if container.IPv4 != nil {
            configBackend.Backend.IPv4 = container.IPv4.String()
        }

        // find potential ports for service by label
        portLabels := []struct{
            proto string
            label string
        }{
            {"tcp", "net.qmsk.clusterf.backend.tcp"},
            {"udp", "net.qmsk.clusterf.backend.udp"},
            {"tcp", fmt.Sprintf("net.qmsk.clusterf.backend:%s.tcp", serviceName)},
            {"udp", fmt.Sprintf("net.qmsk.clusterf.backend:%s.udp", serviceName)},
        }

        for _, portLabel := range portLabels {
            // lookup exposed docker.Port
            portName, labelFound := container.Labels[portLabel.label]
            if !labelFound {
                continue
            }

            port, portFound := containerPorts[fmt.Sprintf("%s:%s", portLabel.proto, portName)]
            if !portFound {
                log.Printf("configContainer %v: service %v port %v is not exposed\n", container, serviceName, portName)
                continue
            }

            // configure
            switch port.Proto {
            case "tcp":
                configBackend.Backend.TCP = port.Port
            case "udp":
                configBackend.Backend.UDP = port.Port
            }
        }

        if configBackend.Backend.TCP != 0 || configBackend.Backend.UDP != 0 {
            configs = append(configs, configBackend)
        }
    }

    return
}
<|endoftext|>


<commit_before>package main

import (
	"flag"
	"fmt"
	"os"
	"time"

	"github.com/Symantec/Dominator/lib/awsutil"
	"github.com/Symantec/Dominator/lib/constants"
	liblog "github.com/Symantec/Dominator/lib/log"
	"github.com/Symantec/Dominator/lib/log/cmdlogger"
	"github.com/Symantec/Dominator/lib/srpc/setupclient"
	libtags "github.com/Symantec/Dominator/lib/tags"
)

var (
	amiName    = flag.String("amiName", "", "AMI Name property")
	enaSupport = flag.Bool("enaSupport", false,
		"If true, set the Enhanced Networking Adaptor capability flag")
	excludeSearchTags libtags.Tags
	expiresIn         = flag.Duration("expiresIn", time.Hour,
		"Date to set for the ExpiresAt tag")
	ignoreMissingUnpackers = flag.Bool("ignoreMissingUnpackers", false,
		"If true, do not generate an error for missing unpackers")
	imageServerHostname = flag.String("imageServerHostname", "localhost",
		"Hostname of imageserver")
	imageServerPortNum = flag.Uint("imageServerPortNum",
		constants.ImageServerPortNumber, "Port number of imageserver")
	instanceName = flag.String("instanceName", "ImageUnpacker",
		"The Name tag value for image unpacker instances")
	instanceType = flag.String("instanceType", "t2.medium",
		"Instance type to launch")
	marketplaceImage = flag.String("marketplaceImage",
		"3f8t6t8fp5m9xx18yzwriozxi",
		"Product code (default Debian Jessie amd64)")
	marketplaceLoginName = flag.String("marketplaceLoginName", "admin",
		"Login name for instance booted from marketplace image")
	maxIdleTime = flag.Duration("maxIdleTime", time.Minute*5,
		"Maximum idle time for image unpacker instances")
	minFreeBytes = flag.Uint64("minFreeBytes", 1<<28,
		"minimum number of free bytes in image")
	minImageAge = flag.Duration("minImageAge", time.Hour*24,
		"Minimum image age when listing or deleting unused images")
	oldImageInstancesCsvFile = flag.String("oldImageInstancesCsvFile", "",
		"File to write CSV listing old image instances")
	replaceInstances = flag.Bool("replaceInstances", false,
		"If true, replace old instances when launching, else skip on old")
	s3Bucket = flag.String("s3Bucket", "",
		"S3 bucket to upload bundle to (default is EBS-backed AMIs)")
	s3Folder = flag.String("s3Folder", "",
		"S3 folder to upload bundle to (default is EBS-backed AMIs)")
	searchTags              = libtags.Tags{"Preferred": "true"}
	securityGroupSearchTags libtags.Tags
	sharingAccountName      = flag.String("sharingAccountName", "",
		"Account from which to share AMIs (for S3-backed)")
	skipTargets awsutil.TargetList
	sshKeyName  = flag.String("sshKeyName", "",
		"Name of SSH key for instance")
	subnetSearchTags    libtags.Tags = libtags.Tags{"Network": "Private"}
	tags                libtags.Tags
	targets             awsutil.TargetList
	unusedImagesCsvFile = flag.String("unusedImagesCsvFile", "",
		"File to write CSV listing unused images")
	vpcSearchTags libtags.Tags = libtags.Tags{"Preferred": "true"}
)

func init() {
	flag.Var(&excludeSearchTags, "excludeSearchTags",
		"Name of exclude tags to use when searching for resources")
	flag.Var(&searchTags, "searchTags",
		"Name of tags to use when searching for resources")
	flag.Var(&securityGroupSearchTags, "securityGroupSearchTags",
		"Restrict security group search to given tags")
	flag.Var(&skipTargets, "skipTargets",
		"List of targets to skip (default none). No wildcards permitted")
	flag.Var(&subnetSearchTags, "subnetSearchTags",
		"Restrict subnet search to given tags")
	flag.Var(&tags, "tags", "Tags to apply")
	flag.Var(&targets, "targets",
		"List of targets (default all accounts and regions)")
	flag.Var(&vpcSearchTags, "vpcSearchTags",
		"Restrict VPC search to given tags")
}

func printUsage() {
	fmt.Fprintln(os.Stderr,
		"Usage: ami-publisher [flags...] publish [args...]")
	fmt.Fprintln(os.Stderr, "Common flags:")
	flag.PrintDefaults()
	fmt.Fprintln(os.Stderr, "Commands:")
	fmt.Fprintln(os.Stderr, "  add-volumes sizeInGiB")
	fmt.Fprintln(os.Stderr, "  copy-bootstrap-image stream-name")
	fmt.Fprintln(os.Stderr, "  delete results-file...")
	fmt.Fprintln(os.Stderr, "  delete-tags tag-key results-file...")
	fmt.Fprintln(os.Stderr, "  delete-tag-on-unpackers tag-key")
	fmt.Fprintln(os.Stderr, "  delete-unused-images")
	fmt.Fprintln(os.Stderr, "  expire")
	fmt.Fprintln(os.Stderr, "  import-key-pair name pub-key-file")
	fmt.Fprintln(os.Stderr, "  launch-instances boot-image")
	fmt.Fprintln(os.Stderr, "  launch-instances-for-images results-file...")
	fmt.Fprintln(os.Stderr, "  list-images")
	fmt.Fprintln(os.Stderr, "  list-streams")
	fmt.Fprintln(os.Stderr, "  list-unpackers")
	fmt.Fprintln(os.Stderr, "  list-unused-images")
	fmt.Fprintln(os.Stderr, "  prepare-unpackers [stream-name]")
	fmt.Fprintln(os.Stderr, "  publish stream-name image-leaf-name")
	fmt.Fprintln(os.Stderr, "  remove-unused-volumes")
	fmt.Fprintln(os.Stderr, "  set-exclusive-tags key value results-file...")
	fmt.Fprintln(os.Stderr, "  set-tags-on-unpackers")
	fmt.Fprintln(os.Stderr, "  start-instances")
	fmt.Fprintln(os.Stderr, "  stop-idle-unpackers")
	fmt.Fprintln(os.Stderr, "  terminate-instances")
}

type commandFunc func([]string, liblog.DebugLogger)

type subcommand struct {
	command string
	minArgs int
	maxArgs int
	cmdFunc commandFunc
}

var subcommands = []subcommand{
	{"add-volumes", 1, 1, addVolumesSubcommand},
	{"copy-bootstrap-image", 1, 1, copyBootstrapImageSubcommand},
	{"delete", 1, -1, deleteSubcommand},
	{"delete-tags", 2, -1, deleteTagsSubcommand},
	{"delete-tags-on-unpackers", 1, 1, deleteTagsOnUnpackersSubcommand},
	{"delete-unused-images", 0, 0, deleteUnusedImagesSubcommand},
	{"expire", 0, 0, expireSubcommand},
	{"import-key-pair", 2, 2, importKeyPairSubcommand},
	{"launch-instances", 1, 1, launchInstancesSubcommand},
	{"launch-instances-for-images", 0, -1, launchInstancesForImagesSubcommand},
	{"list-images", 0, 0, listImagesSubcommand},
	{"list-streams", 0, 0, listStreamsSubcommand},
	{"list-unpackers", 0, 0, listUnpackersSubcommand},
	{"list-unused-images", 0, 0, listUnusedImagesSubcommand},
	{"prepare-unpackers", 0, 1, prepareUnpackersSubcommand},
	{"publish", 2, 2, publishSubcommand},
	{"remove-unused-volumes", 0, 0, removeUnusedVolumesSubcommand},
	{"set-exclusive-tags", 2, -1, setExclusiveTagsSubcommand},
	{"set-tags-on-unpackers", 0, 0, setTagsSubcommand},
	{"start-instances", 0, 0, startInstancesSubcommand},
	{"stop-idle-unpackers", 0, 0, stopIdleUnpackersSubcommand},
	{"terminate-instances", 0, 0, terminateInstancesSubcommand},
}

func main() {
	cmdlogger.SetDatestampsDefault(true)
	flag.Usage = printUsage
	flag.Parse()
	if flag.NArg() < 1 {
		printUsage()
		os.Exit(2)
	}
	logger := cmdlogger.New()
	if err := setupclient.SetupTls(true); err != nil {
		logger.Println(err)
		os.Exit(1)
	}
	numSubcommandArgs := flag.NArg() - 1
	for _, subcommand := range subcommands {
		if flag.Arg(0) == subcommand.command {
			if numSubcommandArgs < subcommand.minArgs ||
				(subcommand.maxArgs >= 0 &&
					numSubcommandArgs > subcommand.maxArgs) {
				printUsage()
				os.Exit(2)
			}
			subcommand.cmdFunc(flag.Args()[1:], logger)
			os.Exit(3)
		}
	}
	printUsage()
	os.Exit(2)
}
<commit_msg>Use lib/flags/loadflags package in ami-publisher.<commit_after>package main

import (
	"flag"
	"fmt"
	"os"
	"time"

	"github.com/Symantec/Dominator/lib/awsutil"
	"github.com/Symantec/Dominator/lib/constants"
	"github.com/Symantec/Dominator/lib/flags/loadflags"
	liblog "github.com/Symantec/Dominator/lib/log"
	"github.com/Symantec/Dominator/lib/log/cmdlogger"
	"github.com/Symantec/Dominator/lib/srpc/setupclient"
	libtags "github.com/Symantec/Dominator/lib/tags"
)

var (
	amiName    = flag.String("amiName", "", "AMI Name property")
	enaSupport = flag.Bool("enaSupport", false,
		"If true, set the Enhanced Networking Adaptor capability flag")
	excludeSearchTags libtags.Tags
	expiresIn         = flag.Duration("expiresIn", time.Hour,
		"Date to set for the ExpiresAt tag")
	ignoreMissingUnpackers = flag.Bool("ignoreMissingUnpackers", false,
		"If true, do not generate an error for missing unpackers")
	imageServerHostname = flag.String("imageServerHostname", "localhost",
		"Hostname of imageserver")
	imageServerPortNum = flag.Uint("imageServerPortNum",
		constants.ImageServerPortNumber, "Port number of imageserver")
	instanceName = flag.String("instanceName", "ImageUnpacker",
		"The Name tag value for image unpacker instances")
	instanceType = flag.String("instanceType", "t2.medium",
		"Instance type to launch")
	marketplaceImage = flag.String("marketplaceImage",
		"3f8t6t8fp5m9xx18yzwriozxi",
		"Product code (default Debian Jessie amd64)")
	marketplaceLoginName = flag.String("marketplaceLoginName", "admin",
		"Login name for instance booted from marketplace image")
	maxIdleTime = flag.Duration("maxIdleTime", time.Minute*5,
		"Maximum idle time for image unpacker instances")
	minFreeBytes = flag.Uint64("minFreeBytes", 1<<28,
		"minimum number of free bytes in image")
	minImageAge = flag.Duration("minImageAge", time.Hour*24,
		"Minimum image age when listing or deleting unused images")
	oldImageInstancesCsvFile = flag.String("oldImageInstancesCsvFile", "",
		"File to write CSV listing old image instances")
	replaceInstances = flag.Bool("replaceInstances", false,
		"If true, replace old instances when launching, else skip on old")
	s3Bucket = flag.String("s3Bucket", "",
		"S3 bucket to upload bundle to (default is EBS-backed AMIs)")
	s3Folder = flag.String("s3Folder", "",
		"S3 folder to upload bundle to (default is EBS-backed AMIs)")
	searchTags              = libtags.Tags{"Preferred": "true"}
	securityGroupSearchTags libtags.Tags
	sharingAccountName      = flag.String("sharingAccountName", "",
		"Account from which to share AMIs (for S3-backed)")
	skipTargets awsutil.TargetList
	sshKeyName  = flag.String("sshKeyName", "",
		"Name of SSH key for instance")
	subnetSearchTags    libtags.Tags = libtags.Tags{"Network": "Private"}
	tags                libtags.Tags
	targets             awsutil.TargetList
	unusedImagesCsvFile = flag.String("unusedImagesCsvFile", "",
		"File to write CSV listing unused images")
	vpcSearchTags libtags.Tags = libtags.Tags{"Preferred": "true"}
)

func init() {
	flag.Var(&excludeSearchTags, "excludeSearchTags",
		"Name of exclude tags to use when searching for resources")
	flag.Var(&searchTags, "searchTags",
		"Name of tags to use when searching for resources")
	flag.Var(&securityGroupSearchTags, "securityGroupSearchTags",
		"Restrict security group search to given tags")
	flag.Var(&skipTargets, "skipTargets",
		"List of targets to skip (default none). No wildcards permitted")
	flag.Var(&subnetSearchTags, "subnetSearchTags",
		"Restrict subnet search to given tags")
	flag.Var(&tags, "tags", "Tags to apply")
	flag.Var(&targets, "targets",
		"List of targets (default all accounts and regions)")
	flag.Var(&vpcSearchTags, "vpcSearchTags",
		"Restrict VPC search to given tags")
}

func printUsage() {
	fmt.Fprintln(os.Stderr,
		"Usage: ami-publisher [flags...] publish [args...]")
	fmt.Fprintln(os.Stderr, "Common flags:")
	flag.PrintDefaults()
	fmt.Fprintln(os.Stderr, "Commands:")
	fmt.Fprintln(os.Stderr, "  add-volumes sizeInGiB")
	fmt.Fprintln(os.Stderr, "  copy-bootstrap-image stream-name")
	fmt.Fprintln(os.Stderr, "  delete results-file...")
	fmt.Fprintln(os.Stderr, "  delete-tags tag-key results-file...")
	fmt.Fprintln(os.Stderr, "  delete-tag-on-unpackers tag-key")
	fmt.Fprintln(os.Stderr, "  delete-unused-images")
	fmt.Fprintln(os.Stderr, "  expire")
	fmt.Fprintln(os.Stderr, "  import-key-pair name pub-key-file")
	fmt.Fprintln(os.Stderr, "  launch-instances boot-image")
	fmt.Fprintln(os.Stderr, "  launch-instances-for-images results-file...")
	fmt.Fprintln(os.Stderr, "  list-images")
	fmt.Fprintln(os.Stderr, "  list-streams")
	fmt.Fprintln(os.Stderr, "  list-unpackers")
	fmt.Fprintln(os.Stderr, "  list-unused-images")
	fmt.Fprintln(os.Stderr, "  prepare-unpackers [stream-name]")
	fmt.Fprintln(os.Stderr, "  publish stream-name image-leaf-name")
	fmt.Fprintln(os.Stderr, "  remove-unused-volumes")
	fmt.Fprintln(os.Stderr, "  set-exclusive-tags key value results-file...")
	fmt.Fprintln(os.Stderr, "  set-tags-on-unpackers")
	fmt.Fprintln(os.Stderr, "  start-instances")
	fmt.Fprintln(os.Stderr, "  stop-idle-unpackers")
	fmt.Fprintln(os.Stderr, "  terminate-instances")
}

type commandFunc func([]string, liblog.DebugLogger)

type subcommand struct {
	command string
	minArgs int
	maxArgs int
	cmdFunc commandFunc
}

var subcommands = []subcommand{
	{"add-volumes", 1, 1, addVolumesSubcommand},
	{"copy-bootstrap-image", 1, 1, copyBootstrapImageSubcommand},
	{"delete", 1, -1, deleteSubcommand},
	{"delete-tags", 2, -1, deleteTagsSubcommand},
	{"delete-tags-on-unpackers", 1, 1, deleteTagsOnUnpackersSubcommand},
	{"delete-unused-images", 0, 0, deleteUnusedImagesSubcommand},
	{"expire", 0, 0, expireSubcommand},
	{"import-key-pair", 2, 2, importKeyPairSubcommand},
	{"launch-instances", 1, 1, launchInstancesSubcommand},
	{"launch-instances-for-images", 0, -1, launchInstancesForImagesSubcommand},
	{"list-images", 0, 0, listImagesSubcommand},
	{"list-streams", 0, 0, listStreamsSubcommand},
	{"list-unpackers", 0, 0, listUnpackersSubcommand},
	{"list-unused-images", 0, 0, listUnusedImagesSubcommand},
	{"prepare-unpackers", 0, 1, prepareUnpackersSubcommand},
	{"publish", 2, 2, publishSubcommand},
	{"remove-unused-volumes", 0, 0, removeUnusedVolumesSubcommand},
	{"set-exclusive-tags", 2, -1, setExclusiveTagsSubcommand},
	{"set-tags-on-unpackers", 0, 0, setTagsSubcommand},
	{"start-instances", 0, 0, startInstancesSubcommand},
	{"stop-idle-unpackers", 0, 0, stopIdleUnpackersSubcommand},
	{"terminate-instances", 0, 0, terminateInstancesSubcommand},
}

func main() {
	if err := loadflags.LoadForCli("ami-publisher"); err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
	cmdlogger.SetDatestampsDefault(true)
	flag.Usage = printUsage
	flag.Parse()
	if flag.NArg() < 1 {
		printUsage()
		os.Exit(2)
	}
	logger := cmdlogger.New()
	if err := setupclient.SetupTls(true); err != nil {
		logger.Println(err)
		os.Exit(1)
	}
	numSubcommandArgs := flag.NArg() - 1
	for _, subcommand := range subcommands {
		if flag.Arg(0) == subcommand.command {
			if numSubcommandArgs < subcommand.minArgs ||
				(subcommand.maxArgs >= 0 &&
					numSubcommandArgs > subcommand.maxArgs) {
				printUsage()
				os.Exit(2)
			}
			subcommand.cmdFunc(flag.Args()[1:], logger)
			os.Exit(3)
		}
	}
	printUsage()
	os.Exit(2)
}
<|endoftext|>


<commit_before>package command

import (
	"bufio"
	"encoding/json"
	"flag"
	"fmt"
	"io/ioutil"
	"os"
	"strconv"
	"strings"
	"time"

	"github.com/funkygao/gafka/ctx"
	"github.com/funkygao/gafka/zk"
	"github.com/funkygao/gocli"
	"github.com/funkygao/golib/color"
	"github.com/funkygao/golib/pipestream"
	"github.com/funkygao/golib/rand"
)

const (
	reassignNodeFilename = "reassignment-node.json"
)

type Migrate struct {
	Ui  cli.Ui
	Cmd string

	zkcluster  *zk.ZkCluster
	zone       string
	cluster    string
	topic      string
	brokerId   string
	partition  string
	verifyMode bool
}

func (this *Migrate) Run(args []string) (exitCode int) {
	cmdFlags := flag.NewFlagSet("migrate", flag.ContinueOnError)
	cmdFlags.Usage = func() { this.Ui.Output(this.Help()) }
	cmdFlags.StringVar(&this.zone, "z", "", "")
	cmdFlags.StringVar(&this.cluster, "c", "", "")
	cmdFlags.StringVar(&this.topic, "t", "", "")
	cmdFlags.StringVar(&this.partition, "p", "", "")
	cmdFlags.StringVar(&this.brokerId, "brokers", "", "")
	cmdFlags.BoolVar(&this.verifyMode, "verify", false, "")
	if err := cmdFlags.Parse(args); err != nil {
		return 1
	}

	if validateArgs(this, this.Ui).
		require("-z", "-c", "-t").
		invalid(args) {
		return 2
	}

	zkzone := zk.NewZkZone(zk.DefaultConfig(this.zone, ctx.ZoneZkAddrs(this.zone)))
	this.zkcluster = zkzone.NewCluster(this.cluster)

	if this.verifyMode {
		this.Ui.Info(fmt.Sprintf("You MUST manually remove the %s after migration is done.", reassignNodeFilename))
		this.Ui.Info(fmt.Sprintf("After verify ok, modify producer/consumer to point to new brokers!"))

		for {
			this.Ui.Output(fmt.Sprintf("%s", time.Now().String()))
			this.verify()

			time.Sleep(time.Second * 2)
		}

		return
	}

	if validateArgs(this, this.Ui).
		require("-z", "-c", "-t", "-p", "-brokers").
		requireAdminRights("-z").
		invalid(args) {
		return 2
	}

	//this.ensureBrokersAreAlive()
	data := this.generateReassignFile()
	this.Ui.Output(data)
	yes, _ := this.Ui.Ask("Are you sure to execute the migration? [Y/N]")
	if yes == "Y" {
		this.executeReassignment()
	} else {
		this.Ui.Output("bye")
	}

	return
}

func (this *Migrate) ensureBrokersAreAlive() {
	brokerIds := strings.Split(this.brokerId, ",")
	liveBrokers := this.zkcluster.Brokers()
	for _, id := range brokerIds {
		if _, present := liveBrokers[id]; !present {
			this.Ui.Error(fmt.Sprintf("broker:%s not alive", id))
			os.Exit(1)
		}
	}

}

func (this *Migrate) normalizePartitions() {
	if strings.Contains(this.partition, "-") {
		// e,g. 0-10
		parts := strings.Split(this.partition, "-")
		startId, err := strconv.Atoi(parts[0])
		swallow(err)
		endId, err := strconv.Atoi(parts[1])
		swallow(err)
		if startId >= endId {
			panic("invalid partition id")
		}

		partitions := make([]string, 0)
		for i := startId; i <= endId; i++ {
			partitions = append(partitions, strconv.Itoa(i))
		}

		this.partition = strings.Join(partitions, ",")
	}
}

// generate reassignment-node.json
func (this *Migrate) generateReassignFile() string {
	// {"version":1,"partitions":[{"topic":"fortest1","partition":0,"replicas":[3,4]}

	type PartitionMeta struct {
		Topic     string `json:"topic"`
		Partition int    `json:"partition"`
		Replicas  []int  `json:"replicas"`
	}
	type ReassignMeta struct {
		Version    int             `json:"version"`
		Partitions []PartitionMeta `json:"partitions"`
	}

	var js ReassignMeta
	js.Version = 1
	js.Partitions = make([]PartitionMeta, 0)
	for _, p := range strings.Split(this.partition, ",") {
		p = strings.TrimSpace(p)
		pid, err := strconv.Atoi(p)
		swallow(err)

		pmeta := PartitionMeta{
			Topic:     this.topic,
			Partition: pid,
			Replicas:  make([]int, 0),
		}
		for _, b := range strings.Split(this.brokerId, ",") {
			b = strings.TrimSpace(b)
			bid, err := strconv.Atoi(b)
			swallow(err)

			pmeta.Replicas = append(pmeta.Replicas, bid)

			// shuffle for load balance the preferred leader
			pmeta.Replicas = rand.ShuffleInts(pmeta.Replicas)
		}

		js.Partitions = append(js.Partitions, pmeta)
	}

	b, err := json.Marshal(js)
	swallow(err)
	swallow(ioutil.WriteFile(reassignNodeFilename, b, 0644))
	return string(b)
}

func (this *Migrate) executeReassignment() {
	/*
		1. kafka-reassign-partitions.sh write /admin/reassign_partitions
		2. controller listens to the path above
		3. For each topic partition, the controller does the following:
		  3.1. Start new replicas in RAR – AR (RAR = Reassigned Replicas, AR = original list of Assigned Replicas)
		  3.2. Wait until new replicas are in sync with the leader
		  3.3. If the leader is not in RAR, elect a new leader from RAR
		  3.4 4. Stop old replicas AR – RAR
		  3.5. Write new AR
		  3.6. Remove partition from the /admin/reassign_partitions path

	*/
	cmd := pipestream.New(fmt.Sprintf("%s/bin/kafka-reassign-partitions.sh", ctx.KafkaHome()),
		fmt.Sprintf("--zookeeper %s", this.zkcluster.ZkConnectAddr()),
		fmt.Sprintf("--reassignment-json-file %s", reassignNodeFilename),
		fmt.Sprintf("--execute"),
	)
	err := cmd.Open()
	if err != nil {
		return
	}
	defer cmd.Close()

	scanner := bufio.NewScanner(cmd.Reader())
	scanner.Split(bufio.ScanLines)
	for scanner.Scan() {
		this.Ui.Output(color.Yellow(scanner.Text()))
	}
}

func (this *Migrate) verify() {
	cmd := pipestream.New(fmt.Sprintf("%s/bin/kafka-reassign-partitions.sh", ctx.KafkaHome()),
		fmt.Sprintf("--zookeeper %s", this.zkcluster.ZkConnectAddr()),
		fmt.Sprintf("--reassignment-json-file %s", reassignNodeFilename),
		fmt.Sprintf("--verify"),
	)
	err := cmd.Open()
	if err != nil {
		return
	}
	defer cmd.Close()

	scanner := bufio.NewScanner(cmd.Reader())
	scanner.Split(bufio.ScanLines)
	for scanner.Scan() {
		if strings.Contains(scanner.Text(), "successfully") {
			this.Ui.Info(scanner.Text())
		} else {
			this.Ui.Warn(scanner.Text())
		}
	}

}

func (*Migrate) Synopsis() string {
	return "Migrate given topic partition to specified broker ids"
}

func (this *Migrate) Help() string {
	help := fmt.Sprintf(`
Usage: %s migrate -z zone -c cluster [options]

    Migrate given topic partition to specified broker ids.

    e,g. migrate partition 0 of order to broker 2 as master and 3 as replica, then verify
      gk migrate -z prod -c trade -t order -p 0 -brokers 2,3
      gk migrate -z prod -c trade -t order -p 0 -brokers 2,3 -verify

Options:

    -t topic

    -p partitionId
      Multiple partition ids seperated by comma or -.
      e,g. -p 0,1
      e,g. -p 2-20

    -brokers id1,id2,idN
      Migrate the topic to given broker ids.
      brokers order is IMPORTANT! The 1st is the preferred leader.

    -verify
      Verify the migration ongoing.

`, this.Cmd)
	return strings.TrimSpace(help)
}
<commit_msg>sleep more during migration verify<commit_after>package command

import (
	"bufio"
	"encoding/json"
	"flag"
	"fmt"
	"io/ioutil"
	"os"
	"strconv"
	"strings"
	"time"

	"github.com/funkygao/gafka/ctx"
	"github.com/funkygao/gafka/zk"
	"github.com/funkygao/gocli"
	"github.com/funkygao/golib/color"
	"github.com/funkygao/golib/pipestream"
	"github.com/funkygao/golib/rand"
)

const (
	reassignNodeFilename = "reassignment-node.json"
)

type Migrate struct {
	Ui  cli.Ui
	Cmd string

	zkcluster  *zk.ZkCluster
	zone       string
	cluster    string
	topic      string
	brokerId   string
	partition  string
	verifyMode bool
}

func (this *Migrate) Run(args []string) (exitCode int) {
	cmdFlags := flag.NewFlagSet("migrate", flag.ContinueOnError)
	cmdFlags.Usage = func() { this.Ui.Output(this.Help()) }
	cmdFlags.StringVar(&this.zone, "z", "", "")
	cmdFlags.StringVar(&this.cluster, "c", "", "")
	cmdFlags.StringVar(&this.topic, "t", "", "")
	cmdFlags.StringVar(&this.partition, "p", "", "")
	cmdFlags.StringVar(&this.brokerId, "brokers", "", "")
	cmdFlags.BoolVar(&this.verifyMode, "verify", false, "")
	if err := cmdFlags.Parse(args); err != nil {
		return 1
	}

	if validateArgs(this, this.Ui).
		require("-z", "-c", "-t").
		invalid(args) {
		return 2
	}

	zkzone := zk.NewZkZone(zk.DefaultConfig(this.zone, ctx.ZoneZkAddrs(this.zone)))
	this.zkcluster = zkzone.NewCluster(this.cluster)

	if this.verifyMode {
		this.Ui.Info(fmt.Sprintf("You MUST manually remove the %s after migration is done.", reassignNodeFilename))
		this.Ui.Info(fmt.Sprintf("After verify ok, modify producer/consumer to point to new brokers!"))

		for {
			this.Ui.Output(fmt.Sprintf("%s", time.Now().String()))
			this.verify()

			time.Sleep(time.Second * 5)
		}

		return
	}

	if validateArgs(this, this.Ui).
		require("-z", "-c", "-t", "-p", "-brokers").
		requireAdminRights("-z").
		invalid(args) {
		return 2
	}

	//this.ensureBrokersAreAlive()
	data := this.generateReassignFile()
	this.Ui.Output(data)
	yes, _ := this.Ui.Ask("Are you sure to execute the migration? [Y/N]")
	if yes == "Y" {
		this.executeReassignment()
	} else {
		this.Ui.Output("bye")
	}

	return
}

func (this *Migrate) ensureBrokersAreAlive() {
	brokerIds := strings.Split(this.brokerId, ",")
	liveBrokers := this.zkcluster.Brokers()
	for _, id := range brokerIds {
		if _, present := liveBrokers[id]; !present {
			this.Ui.Error(fmt.Sprintf("broker:%s not alive", id))
			os.Exit(1)
		}
	}

}

func (this *Migrate) normalizePartitions() {
	if strings.Contains(this.partition, "-") {
		// e,g. 0-10
		parts := strings.Split(this.partition, "-")
		startId, err := strconv.Atoi(parts[0])
		swallow(err)
		endId, err := strconv.Atoi(parts[1])
		swallow(err)
		if startId >= endId {
			panic("invalid partition id")
		}

		partitions := make([]string, 0)
		for i := startId; i <= endId; i++ {
			partitions = append(partitions, strconv.Itoa(i))
		}

		this.partition = strings.Join(partitions, ",")
	}
}

// generate reassignment-node.json
func (this *Migrate) generateReassignFile() string {
	// {"version":1,"partitions":[{"topic":"fortest1","partition":0,"replicas":[3,4]}

	type PartitionMeta struct {
		Topic     string `json:"topic"`
		Partition int    `json:"partition"`
		Replicas  []int  `json:"replicas"`
	}
	type ReassignMeta struct {
		Version    int             `json:"version"`
		Partitions []PartitionMeta `json:"partitions"`
	}

	var js ReassignMeta
	js.Version = 1
	js.Partitions = make([]PartitionMeta, 0)
	for _, p := range strings.Split(this.partition, ",") {
		p = strings.TrimSpace(p)
		pid, err := strconv.Atoi(p)
		swallow(err)

		pmeta := PartitionMeta{
			Topic:     this.topic,
			Partition: pid,
			Replicas:  make([]int, 0),
		}
		for _, b := range strings.Split(this.brokerId, ",") {
			b = strings.TrimSpace(b)
			bid, err := strconv.Atoi(b)
			swallow(err)

			pmeta.Replicas = append(pmeta.Replicas, bid)

			// shuffle for load balance the preferred leader
			pmeta.Replicas = rand.ShuffleInts(pmeta.Replicas)
		}

		js.Partitions = append(js.Partitions, pmeta)
	}

	b, err := json.Marshal(js)
	swallow(err)
	swallow(ioutil.WriteFile(reassignNodeFilename, b, 0644))
	return string(b)
}

func (this *Migrate) executeReassignment() {
	/*
		1. kafka-reassign-partitions.sh write /admin/reassign_partitions
		2. controller listens to the path above
		3. For each topic partition, the controller does the following:
		  3.1. Start new replicas in RAR – AR (RAR = Reassigned Replicas, AR = original list of Assigned Replicas)
		  3.2. Wait until new replicas are in sync with the leader
		  3.3. If the leader is not in RAR, elect a new leader from RAR
		  3.4 4. Stop old replicas AR – RAR
		  3.5. Write new AR
		  3.6. Remove partition from the /admin/reassign_partitions path

	*/
	cmd := pipestream.New(fmt.Sprintf("%s/bin/kafka-reassign-partitions.sh", ctx.KafkaHome()),
		fmt.Sprintf("--zookeeper %s", this.zkcluster.ZkConnectAddr()),
		fmt.Sprintf("--reassignment-json-file %s", reassignNodeFilename),
		fmt.Sprintf("--execute"),
	)
	err := cmd.Open()
	if err != nil {
		return
	}
	defer cmd.Close()

	scanner := bufio.NewScanner(cmd.Reader())
	scanner.Split(bufio.ScanLines)
	for scanner.Scan() {
		this.Ui.Output(color.Yellow(scanner.Text()))
	}
}

func (this *Migrate) verify() {
	cmd := pipestream.New(fmt.Sprintf("%s/bin/kafka-reassign-partitions.sh", ctx.KafkaHome()),
		fmt.Sprintf("--zookeeper %s", this.zkcluster.ZkConnectAddr()),
		fmt.Sprintf("--reassignment-json-file %s", reassignNodeFilename),
		fmt.Sprintf("--verify"),
	)
	err := cmd.Open()
	if err != nil {
		return
	}
	defer cmd.Close()

	scanner := bufio.NewScanner(cmd.Reader())
	scanner.Split(bufio.ScanLines)
	for scanner.Scan() {
		if strings.Contains(scanner.Text(), "successfully") {
			this.Ui.Info(scanner.Text())
		} else {
			this.Ui.Warn(scanner.Text())
		}
	}

}

func (*Migrate) Synopsis() string {
	return "Migrate given topic partition to specified broker ids"
}

func (this *Migrate) Help() string {
	help := fmt.Sprintf(`
Usage: %s migrate -z zone -c cluster [options]

    Migrate given topic partition to specified broker ids.

    e,g. migrate partition 0 of order to broker 2 as master and 3 as replica, then verify
      gk migrate -z prod -c trade -t order -p 0 -brokers 2,3
      gk migrate -z prod -c trade -t order -p 0 -brokers 2,3 -verify

Options:

    -t topic

    -p partitionId
      Multiple partition ids seperated by comma or -.
      e,g. -p 0,1
      e,g. -p 2-20

    -brokers id1,id2,idN
      Migrate the topic to given broker ids.
      brokers order is IMPORTANT! The 1st is the preferred leader.

    -verify
      Verify the migration ongoing.

`, this.Cmd)
	return strings.TrimSpace(help)
}
<|endoftext|>


<commit_before>/*
Copyright 2016 The Kubernetes Authors All rights reserved.
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 (
	"errors"
	"fmt"
	"io"
	"os"

	"k8s.io/helm/pkg/helm/helmpath"
	"k8s.io/helm/pkg/plugin"

	"github.com/spf13/cobra"
)

type pluginRemoveCmd struct {
	names []string
	home  helmpath.Home
	out   io.Writer
}

func newPluginRemoveCmd(out io.Writer) *cobra.Command {
	pcmd := &pluginRemoveCmd{out: out}
	cmd := &cobra.Command{
		Use:   "remove <plugin>...",
		Short: "remove one or more Helm plugins",
		PreRunE: func(cmd *cobra.Command, args []string) error {
			return pcmd.complete(args)
		},
		RunE: func(cmd *cobra.Command, args []string) error {
			return pcmd.run()
		},
	}
	return cmd
}

func (pcmd *pluginRemoveCmd) complete(args []string) error {
	if len(args) == 0 {
		return errors.New("please provide plugin name to remove")
	}
	pcmd.names = args
	pcmd.home = settings.Home
	return nil
}

func (pcmd *pluginRemoveCmd) run() error {
	plugdirs := pluginDirs(pcmd.home)
	debug("loading installed plugins from %s", plugdirs)
	plugins, err := findPlugins(plugdirs)
	if err != nil {
		return err
	}

	for _, name := range pcmd.names {
		if found := findPlugin(plugins, name); found != nil {
			if err := removePlugin(found, pcmd.home); err != nil {
				fmt.Fprintf(pcmd.out, "Failed to remove plugin %s, got error (%v)\n", name, err)
			} else {
				fmt.Fprintf(pcmd.out, "Removed plugin: %s\n", name)
			}
		} else {
			fmt.Fprintf(pcmd.out, "Plugin: %s not found\n", name)
		}
	}
	return nil
}

func removePlugin(p *plugin.Plugin, home helmpath.Home) error {
	if err := os.Remove(p.Dir); err != nil {
		return err
	}
	return runHook(p, plugin.Delete, home)
}

func findPlugin(plugins []*plugin.Plugin, name string) *plugin.Plugin {
	for _, p := range plugins {
		if p.Metadata.Name == name {
			return p
		}
	}
	return nil
}
<commit_msg>Return error exit-code in case of error<commit_after>/*
Copyright 2016 The Kubernetes Authors All rights reserved.
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 (
	"errors"
	"fmt"
	"io"
	"os"
	"strings"

	"k8s.io/helm/pkg/helm/helmpath"
	"k8s.io/helm/pkg/plugin"

	"github.com/spf13/cobra"
)

type pluginRemoveCmd struct {
	names []string
	home  helmpath.Home
	out   io.Writer
}

func newPluginRemoveCmd(out io.Writer) *cobra.Command {
	pcmd := &pluginRemoveCmd{out: out}
	cmd := &cobra.Command{
		Use:   "remove <plugin>...",
		Short: "remove one or more Helm plugins",
		PreRunE: func(cmd *cobra.Command, args []string) error {
			return pcmd.complete(args)
		},
		RunE: func(cmd *cobra.Command, args []string) error {
			return pcmd.run()
		},
	}
	return cmd
}

func (pcmd *pluginRemoveCmd) complete(args []string) error {
	if len(args) == 0 {
		return errors.New("please provide plugin name to remove")
	}
	pcmd.names = args
	pcmd.home = settings.Home
	return nil
}

func (pcmd *pluginRemoveCmd) run() error {
	plugdirs := pluginDirs(pcmd.home)
	debug("loading installed plugins from %s", plugdirs)
	plugins, err := findPlugins(plugdirs)
	if err != nil {
		return err
	}
	var errorPlugins []string
	for _, name := range pcmd.names {
		if found := findPlugin(plugins, name); found != nil {
			if err := removePlugin(found, pcmd.home); err != nil {
				errorPlugins = append(errorPlugins, fmt.Sprintf("Failed to remove plugin %s, got error (%v)", name, err))
			} else {
				fmt.Fprintf(pcmd.out, "Removed plugin: %s\n", name)
			}
		} else {
			errorPlugins = append(errorPlugins, fmt.Sprintf("Plugin: %s not found", name))
		}
	}
	if len(errorPlugins) > 0 {
		return fmt.Errorf(strings.Join(errorPlugins, "\n"))
	}
	return nil
}

func removePlugin(p *plugin.Plugin, home helmpath.Home) error {
	if err := os.Remove(p.Dir); err != nil {
		return err
	}
	return runHook(p, plugin.Delete, home)
}

func findPlugin(plugins []*plugin.Plugin, name string) *plugin.Plugin {
	for _, p := range plugins {
		if p.Metadata.Name == name {
			return p
		}
	}
	return nil
}
<|endoftext|>


<commit_before>/*
Copyright 2020 The Kubernetes 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 (
	"context"
	"encoding/json"
	"fmt"
	"io"
	"strings"

	"k8s.io/kops/pkg/cloudinstances"
	"k8s.io/kops/pkg/commands/commandutils"
	"k8s.io/kubectl/pkg/util/i18n"
	"k8s.io/kubectl/pkg/util/templates"
	"sigs.k8s.io/yaml"

	"k8s.io/klog/v2"

	"k8s.io/client-go/kubernetes"

	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/cli-runtime/pkg/genericclioptions"

	"k8s.io/kops/util/pkg/tables"

	"k8s.io/kops/pkg/apis/kops"

	"github.com/spf13/cobra"
	"k8s.io/kops/cmd/kops/util"
	"k8s.io/kops/upup/pkg/fi/cloudup"
)

var (
	getInstancesExample = templates.Examples(i18n.T(`
	# Display all instances.
	kops get instances
	`))

	getInstancesShort = i18n.T(`Display cluster instances.`)
)

type renderableCloudInstance struct {
	ID            string   `json:"id"`
	NodeName      string   `json:"nodeName"`
	Status        string   `json:"status"`
	Roles         []string `json:"roles"`
	InternalIP    string   `json:"internalIP"`
	InstanceGroup string   `json:"instanceGroup"`
	MachineType   string   `json:"machineType"`
	State         string   `json:"state"`
}

func NewCmdGetInstances(f *util.Factory, out io.Writer, options *GetOptions) *cobra.Command {
	cmd := &cobra.Command{
		Use:               "instances [CLUSTER]",
		Short:             getInstancesShort,
		Example:           getInstancesExample,
		Args:              rootCommand.clusterNameArgs(&options.ClusterName),
		ValidArgsFunction: commandutils.CompleteClusterName(f, true, false),
		RunE: func(cmd *cobra.Command, args []string) error {
			return RunGetInstances(context.TODO(), f, out, options)
		},
	}

	return cmd
}

func RunGetInstances(ctx context.Context, f *util.Factory, out io.Writer, options *GetOptions) error {
	clientset, err := f.Clientset()
	if err != nil {
		return err
	}

	cluster, err := clientset.GetCluster(ctx, options.ClusterName)
	if err != nil {
		return err
	}

	if cluster == nil {
		return fmt.Errorf("cluster not found %q", options.ClusterName)
	}

	cloud, err := cloudup.BuildCloud(cluster)
	if err != nil {
		return err
	}

	k8sClient, err := createK8sClient(cluster)
	if err != nil {
		return err
	}

	nodeList, err := k8sClient.CoreV1().Nodes().List(ctx, metav1.ListOptions{})
	if err != nil {
		klog.Warningf("cannot list node names. Kubernetes API unavailable: %v", err)
	}

	igList, err := clientset.InstanceGroupsFor(cluster).List(ctx, metav1.ListOptions{})
	if err != nil {
		return err
	}

	var instanceGroups []*kops.InstanceGroup
	for i := range igList.Items {
		instanceGroups = append(instanceGroups, &igList.Items[i])
	}

	var cloudInstances []*cloudinstances.CloudInstance

	cloudGroups, err := cloud.GetCloudGroups(cluster, instanceGroups, false, nodeList.Items)
	if err != nil {
		return err
	}

	for _, cg := range cloudGroups {
		cloudInstances = append(cloudInstances, cg.Ready...)
		cloudInstances = append(cloudInstances, cg.NeedUpdate...)
		cg.AdjustNeedUpdate()
	}

	switch options.Output {
	case OutputTable:
		return instanceOutputTable(cloudInstances, out)
	case OutputYaml:
		y, err := yaml.Marshal(asRenderable(cloudInstances))
		if err != nil {
			return fmt.Errorf("unable to marshal YAML: %v", err)
		}
		if _, err := out.Write(y); err != nil {
			return fmt.Errorf("error writing to output: %v", err)
		}
		return nil
	case OutputJSON:
		j, err := json.Marshal(asRenderable(cloudInstances))
		if err != nil {
			return fmt.Errorf("unable to marshal JSON: %v", err)
		}
		if _, err := out.Write(j); err != nil {
			return fmt.Errorf("error writing to output: %v", err)
		}
		return nil
	default:
		return fmt.Errorf("unsupported output format: %q", options.Output)
	}
}

func instanceOutputTable(instances []*cloudinstances.CloudInstance, out io.Writer) error {
	fmt.Println("")
	t := &tables.Table{}
	t.AddColumn("ID", func(i *cloudinstances.CloudInstance) string {
		return i.ID
	})
	t.AddColumn("NODE-NAME", func(i *cloudinstances.CloudInstance) string {
		node := i.Node
		if node == nil {
			return ""
		} else {
			return node.Name
		}
	})
	t.AddColumn("STATUS", func(i *cloudinstances.CloudInstance) string {
		return i.Status
	})
	t.AddColumn("ROLES", func(i *cloudinstances.CloudInstance) string {
		return strings.Join(i.Roles, ", ")
	})
	t.AddColumn("INTERNAL-IP", func(i *cloudinstances.CloudInstance) string {
		return i.PrivateIP
	})
	t.AddColumn("INSTANCE-GROUP", func(i *cloudinstances.CloudInstance) string {
		return i.CloudInstanceGroup.HumanName
	})
	t.AddColumn("MACHINE-TYPE", func(i *cloudinstances.CloudInstance) string {
		return i.MachineType
	})
	t.AddColumn("STATE", func(i *cloudinstances.CloudInstance) string {
		return string(i.State)
	})

	columns := []string{"ID", "NODE-NAME", "STATUS", "ROLES", "STATE", "INTERNAL-IP", "INSTANCE-GROUP", "MACHINE-TYPE"}
	return t.Render(instances, out, columns...)
}

func createK8sClient(cluster *kops.Cluster) (*kubernetes.Clientset, error) {
	contextName := cluster.ObjectMeta.Name
	clientGetter := genericclioptions.NewConfigFlags(true)
	clientGetter.Context = &contextName

	config, err := clientGetter.ToRESTConfig()
	if err != nil {
		return nil, fmt.Errorf("cannot load kubecfg settings for %q: %v", contextName, err)
	}
	k8sClient, err := kubernetes.NewForConfig(config)
	if err != nil {
		return nil, fmt.Errorf("cannot build kubernetes api client for %q: %v", contextName, err)
	}
	return k8sClient, nil
}

func asRenderable(instances []*cloudinstances.CloudInstance) []*renderableCloudInstance {
	arr := make([]*renderableCloudInstance, len(instances))
	for i, ci := range instances {
		arr[i] = &renderableCloudInstance{
			ID:            ci.ID,
			NodeName:      ci.Node.Name,
			Status:        ci.Status,
			Roles:         ci.Roles,
			InternalIP:    ci.PrivateIP,
			InstanceGroup: ci.CloudInstanceGroup.HumanName,
			MachineType:   ci.MachineType,
			State:         string(ci.State),
		}
	}
	return arr
}
<commit_msg>Don't try to add node name to instances without node object<commit_after>/*
Copyright 2020 The Kubernetes 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 (
	"context"
	"encoding/json"
	"fmt"
	"io"
	"strings"

	"k8s.io/kops/pkg/cloudinstances"
	"k8s.io/kops/pkg/commands/commandutils"
	"k8s.io/kubectl/pkg/util/i18n"
	"k8s.io/kubectl/pkg/util/templates"
	"sigs.k8s.io/yaml"

	"k8s.io/klog/v2"

	"k8s.io/client-go/kubernetes"

	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/cli-runtime/pkg/genericclioptions"

	"k8s.io/kops/util/pkg/tables"

	"k8s.io/kops/pkg/apis/kops"

	"github.com/spf13/cobra"
	"k8s.io/kops/cmd/kops/util"
	"k8s.io/kops/upup/pkg/fi/cloudup"
)

var (
	getInstancesExample = templates.Examples(i18n.T(`
	# Display all instances.
	kops get instances
	`))

	getInstancesShort = i18n.T(`Display cluster instances.`)
)

type renderableCloudInstance struct {
	ID            string   `json:"id"`
	NodeName      string   `json:"nodeName,omitempty"`
	Status        string   `json:"status"`
	Roles         []string `json:"roles"`
	InternalIP    string   `json:"internalIP"`
	InstanceGroup string   `json:"instanceGroup"`
	MachineType   string   `json:"machineType"`
	State         string   `json:"state"`
}

func NewCmdGetInstances(f *util.Factory, out io.Writer, options *GetOptions) *cobra.Command {
	cmd := &cobra.Command{
		Use:               "instances [CLUSTER]",
		Short:             getInstancesShort,
		Example:           getInstancesExample,
		Args:              rootCommand.clusterNameArgs(&options.ClusterName),
		ValidArgsFunction: commandutils.CompleteClusterName(f, true, false),
		RunE: func(cmd *cobra.Command, args []string) error {
			return RunGetInstances(context.TODO(), f, out, options)
		},
	}

	return cmd
}

func RunGetInstances(ctx context.Context, f *util.Factory, out io.Writer, options *GetOptions) error {
	clientset, err := f.Clientset()
	if err != nil {
		return err
	}

	cluster, err := clientset.GetCluster(ctx, options.ClusterName)
	if err != nil {
		return err
	}

	if cluster == nil {
		return fmt.Errorf("cluster not found %q", options.ClusterName)
	}

	cloud, err := cloudup.BuildCloud(cluster)
	if err != nil {
		return err
	}

	k8sClient, err := createK8sClient(cluster)
	if err != nil {
		return err
	}

	nodeList, err := k8sClient.CoreV1().Nodes().List(ctx, metav1.ListOptions{})
	if err != nil {
		klog.Warningf("cannot list node names. Kubernetes API unavailable: %v", err)
	}

	igList, err := clientset.InstanceGroupsFor(cluster).List(ctx, metav1.ListOptions{})
	if err != nil {
		return err
	}

	var instanceGroups []*kops.InstanceGroup
	for i := range igList.Items {
		instanceGroups = append(instanceGroups, &igList.Items[i])
	}

	var cloudInstances []*cloudinstances.CloudInstance

	cloudGroups, err := cloud.GetCloudGroups(cluster, instanceGroups, false, nodeList.Items)
	if err != nil {
		return err
	}

	for _, cg := range cloudGroups {
		cloudInstances = append(cloudInstances, cg.Ready...)
		cloudInstances = append(cloudInstances, cg.NeedUpdate...)
		cg.AdjustNeedUpdate()
	}

	switch options.Output {
	case OutputTable:
		return instanceOutputTable(cloudInstances, out)
	case OutputYaml:
		y, err := yaml.Marshal(asRenderable(cloudInstances))
		if err != nil {
			return fmt.Errorf("unable to marshal YAML: %v", err)
		}
		if _, err := out.Write(y); err != nil {
			return fmt.Errorf("error writing to output: %v", err)
		}
		return nil
	case OutputJSON:
		j, err := json.Marshal(asRenderable(cloudInstances))
		if err != nil {
			return fmt.Errorf("unable to marshal JSON: %v", err)
		}
		if _, err := out.Write(j); err != nil {
			return fmt.Errorf("error writing to output: %v", err)
		}
		return nil
	default:
		return fmt.Errorf("unsupported output format: %q", options.Output)
	}
}

func instanceOutputTable(instances []*cloudinstances.CloudInstance, out io.Writer) error {
	fmt.Println("")
	t := &tables.Table{}
	t.AddColumn("ID", func(i *cloudinstances.CloudInstance) string {
		return i.ID
	})
	t.AddColumn("NODE-NAME", func(i *cloudinstances.CloudInstance) string {
		node := i.Node
		if node == nil {
			return ""
		} else {
			return node.Name
		}
	})
	t.AddColumn("STATUS", func(i *cloudinstances.CloudInstance) string {
		return i.Status
	})
	t.AddColumn("ROLES", func(i *cloudinstances.CloudInstance) string {
		return strings.Join(i.Roles, ", ")
	})
	t.AddColumn("INTERNAL-IP", func(i *cloudinstances.CloudInstance) string {
		return i.PrivateIP
	})
	t.AddColumn("INSTANCE-GROUP", func(i *cloudinstances.CloudInstance) string {
		return i.CloudInstanceGroup.HumanName
	})
	t.AddColumn("MACHINE-TYPE", func(i *cloudinstances.CloudInstance) string {
		return i.MachineType
	})
	t.AddColumn("STATE", func(i *cloudinstances.CloudInstance) string {
		return string(i.State)
	})

	columns := []string{"ID", "NODE-NAME", "STATUS", "ROLES", "STATE", "INTERNAL-IP", "INSTANCE-GROUP", "MACHINE-TYPE"}
	return t.Render(instances, out, columns...)
}

func createK8sClient(cluster *kops.Cluster) (*kubernetes.Clientset, error) {
	contextName := cluster.ObjectMeta.Name
	clientGetter := genericclioptions.NewConfigFlags(true)
	clientGetter.Context = &contextName

	config, err := clientGetter.ToRESTConfig()
	if err != nil {
		return nil, fmt.Errorf("cannot load kubecfg settings for %q: %v", contextName, err)
	}
	k8sClient, err := kubernetes.NewForConfig(config)
	if err != nil {
		return nil, fmt.Errorf("cannot build kubernetes api client for %q: %v", contextName, err)
	}
	return k8sClient, nil
}

func asRenderable(instances []*cloudinstances.CloudInstance) []*renderableCloudInstance {
	arr := make([]*renderableCloudInstance, len(instances))
	for i, ci := range instances {
		arr[i] = &renderableCloudInstance{
			ID:            ci.ID,
			Status:        ci.Status,
			Roles:         ci.Roles,
			InternalIP:    ci.PrivateIP,
			InstanceGroup: ci.CloudInstanceGroup.HumanName,
			MachineType:   ci.MachineType,
			State:         string(ci.State),
		}
		if ci.Node != nil {
			arr[i].NodeName = ci.Node.Name
		}
	}
	return arr
}
<|endoftext|>


<commit_before>package main

import (
	"bytes"
	"flag"
	"fmt"
	"go/format"
	"io/ioutil"
	"os"
	"path/filepath"
	"strings"
	"text/template"
	"time"
)

type templateArg struct {
	CmdPackage string
	Name       string
	Year       int
	Commands   []templateOneCmdArg
}

type templateOneCmdArg struct {
	CmdNameLiteral string // e.g. list-something
	CmdName        string // e.g. listsomething
	*templateArg
}

func main() {
	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, "usage: newjujuplugin <packagepath>/cmd/juju-<name> [cmd...]\n")
		os.Exit(2)
	}
	flag.Parse()
	if flag.NArg() < 1 {
		flag.Usage()
	}
	cmdPackage := flag.Arg(0)
	lastHyphen := strings.LastIndex(cmdPackage, "-")
	if lastHyphen == -1 {
		fail("package name in wrong form")
	}
	commands := flag.Args()[1:]
	arg := &templateArg{
		CmdPackage: flag.Arg(0),
		Name:       cmdPackage[lastHyphen+1:],
		Year:       time.Now().Year(),
	}
	for _, c := range commands {
		arg.Commands = append(arg.Commands, templateOneCmdArg{
			CmdNameLiteral: c,
			CmdName:        fromLiteral(c),
			templateArg:    arg,
		})
	}
	gopath := os.Getenv("GOPATH")
	if i := strings.Index(gopath, ":"); i > 0 {
		gopath = gopath[0:i]
	}
	dir := filepath.Join(gopath, "src", filepath.FromSlash(cmdPackage))
	writeFile(arg, dir, "main.go", mainTemplate)

	cmdDir := filepath.Join(dir, arg.Name+"cmd")
	writeFile(arg, cmdDir, "cmd.go", cmdTemplate)
	writeFile(arg, cmdDir, "cmd_test.go", cmdtestTemplate)
	writeFile(arg, cmdDir, "package_test.go", packagetestTemplate)

	for _, c := range arg.Commands {
		writeFile(c, cmdDir, c.CmdNameLiteral+".go", onecmdTemplate)
		writeFile(c, cmdDir, c.CmdNameLiteral+"_test.go", onecmdtestTemplate)
	}
	fmt.Println(dir)
}

func fromLiteral(s string) string {
	return strings.Replace(s, "-", "", -1)
}

var templateFuncs = template.FuncMap{
	"fromLiteral": fromLiteral,
}

func newTemplate(s string) *template.Template {
	return template.Must(template.New("").Funcs(templateFuncs).Parse(strings.TrimLeft(s, "\n")))
}

func writeFile(arg interface{}, dir, file string, template *template.Template) {
	var buf bytes.Buffer
	if err := os.MkdirAll(dir, 0777); err != nil {
		fail("%v", err)
	}
	if err := template.Execute(&buf, arg); err != nil {
		fail("cannot execute template for %s: %v", file, err)
	}
	path := filepath.Join(dir, file)
	data, err := format.Source(buf.Bytes())
	if err != nil {
		fail("invalid source generated for %s: %v", path, err)
	}
	if err := ioutil.WriteFile(path, data, 0777); err != nil {
		fail("%v", err)
	}
}

func fail(f string, a ...interface{}) {
	fmt.Fprintf(os.Stderr, "%s\n", fmt.Sprintf(f, a...))
	os.Exit(1)
}
<commit_msg>cmd/newjujuplugin: use camel case for command identifiers<commit_after>package main

import (
	"bytes"
	"flag"
	"fmt"
	"go/format"
	"io/ioutil"
	"os"
	"path/filepath"
	"strings"
	"text/template"
	"time"
	"unicode"
)

type templateArg struct {
	CmdPackage string
	Name       string
	Year       int
	Commands   []templateOneCmdArg
}

type templateOneCmdArg struct {
	CmdNameLiteral string // e.g. list-something
	CmdName        string // e.g. listsomething
	*templateArg
}

func main() {
	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, "usage: newjujuplugin <packagepath>/cmd/juju-<name> [cmd...]\n")
		os.Exit(2)
	}
	flag.Parse()
	if flag.NArg() < 1 {
		flag.Usage()
	}
	cmdPackage := flag.Arg(0)
	lastHyphen := strings.LastIndex(cmdPackage, "-")
	if lastHyphen == -1 {
		fail("package name in wrong form")
	}
	commands := flag.Args()[1:]
	arg := &templateArg{
		CmdPackage: flag.Arg(0),
		Name:       cmdPackage[lastHyphen+1:],
		Year:       time.Now().Year(),
	}
	for _, c := range commands {
		arg.Commands = append(arg.Commands, templateOneCmdArg{
			CmdNameLiteral: c,
			CmdName:        fromLiteral(c),
			templateArg:    arg,
		})
	}
	gopath := os.Getenv("GOPATH")
	if i := strings.Index(gopath, ":"); i > 0 {
		gopath = gopath[0:i]
	}
	dir := filepath.Join(gopath, "src", filepath.FromSlash(cmdPackage))
	writeFile(arg, dir, "main.go", mainTemplate)

	cmdDir := filepath.Join(dir, arg.Name+"cmd")
	writeFile(arg, cmdDir, "cmd.go", cmdTemplate)
	writeFile(arg, cmdDir, "cmd_test.go", cmdtestTemplate)
	writeFile(arg, cmdDir, "package_test.go", packagetestTemplate)

	for _, c := range arg.Commands {
		writeFile(c, cmdDir, c.CmdNameLiteral+".go", onecmdTemplate)
		writeFile(c, cmdDir, c.CmdNameLiteral+"_test.go", onecmdtestTemplate)
	}
	fmt.Println(dir)
}

func fromLiteral(s string) string {
	return toCamelCase(s)
}

var templateFuncs = template.FuncMap{
	"fromLiteral": fromLiteral,
}

func newTemplate(s string) *template.Template {
	return template.Must(template.New("").Funcs(templateFuncs).Parse(strings.TrimLeft(s, "\n")))
}

func writeFile(arg interface{}, dir, file string, template *template.Template) {
	var buf bytes.Buffer
	if err := os.MkdirAll(dir, 0777); err != nil {
		fail("%v", err)
	}
	if err := template.Execute(&buf, arg); err != nil {
		fail("cannot execute template for %s: %v", file, err)
	}
	path := filepath.Join(dir, file)
	data, err := format.Source(buf.Bytes())
	if err != nil {
		fail("invalid source generated for %s: %v", path, err)
	}
	if err := ioutil.WriteFile(path, data, 0777); err != nil {
		fail("%v", err)
	}
}

func fail(f string, a ...interface{}) {
	fmt.Fprintf(os.Stderr, "%s\n", fmt.Sprintf(f, a...))
	os.Exit(1)
}

func toCamelCase(s string) (r string) {
	if s == "" {
		return s
	}
	wasHyphen := false
	var out []rune
	for _, r := range s {
		if r == '-' {
			wasHyphen = true
			continue
		}
		if wasHyphen {
			r = unicode.ToUpper(r)
		}
		out = append(out, r)
		wasHyphen = false
	}
	return string(out)
}
<|endoftext|>