Пример #1
0
// execureResourceAction fans out the specific resource action to the appropriate method
// on the ResourceManager for the specific resource.
func executeResourceAction(args map[string]interface{}, client *client.Client, resource unversioned.Resource, action action) (unversioned.Resource, error) {
	rm := resourcemgr.GetResourceManager(resource)
	var err error
	var resourceOut unversioned.Resource

	switch action {
	case actionApply:
		resourceOut, err = rm.Apply(client, resource)
	case actionCreate:
		resourceOut, err = rm.Create(client, resource)
	case actionUpdate:
		resourceOut, err = rm.Update(client, resource)
	case actionDelete:
		resourceOut, err = rm.Delete(client, resource)
	case actionList:
		resourceOut, err = rm.List(client, resource)
	}

	// Skip over some errors depending on command line options.
	if err != nil {
		skip := false
		switch err.(type) {
		case calicoErrors.ErrorResourceAlreadyExists:
			skip = argutils.ArgBoolOrFalse(args, "--skip-exists")
		case calicoErrors.ErrorResourceDoesNotExist:
			skip = argutils.ArgBoolOrFalse(args, "--skip-not-exists")
		}
		if skip {
			resourceOut = resource
			err = nil
		}
	}

	return resourceOut, err
}
Пример #2
0
func (r resourcePrinterTable) print(client *client.Client, resources []unversioned.Resource) error {
	log.Infof("Output in table format (wide=%v)", r.wide)
	for _, resource := range resources {
		// Get the resource manager for the resource type.
		rm := resourcemgr.GetResourceManager(resource)

		// If no headings have been specified then we must be using the default
		// headings for that resource type.
		headings := r.headings
		if r.headings == nil {
			headings = rm.GetTableDefaultHeadings(r.wide)
		}

		// Look up the template string for the specific resource type.
		tpls, err := rm.GetTableTemplate(headings)
		if err != nil {
			return err
		}

		// Convert the template string into a template - we need to include the join
		// function.
		fns := template.FuncMap{
			"join":   join,
			"config": config(client),
		}
		tmpl, err := template.New("get").Funcs(fns).Parse(tpls)
		if err != nil {
			panic(err)
		}

		// Use a tabwriter to write out the teplate - this provides better formatting.
		writer := tabwriter.NewWriter(os.Stdout, 5, 1, 3, ' ', 0)
		err = tmpl.Execute(writer, resource)
		if err != nil {
			panic(err)
		}
		writer.Flush()

		// Templates for ps format are internally defined and therefore we should not
		// hit errors writing the table formats.
		if err != nil {
			panic(err)
		}

		// Leave a gap after each table.
		fmt.Printf("\n")
	}
	return nil
}