示例#1
0
func runTermCmd(api *cait.ArchivesSpaceAPI, cmd *command) (string, error) {
	if err := api.Login(); err != nil {
		return "", err
	}
	term := new(cait.Term)
	if cmd.Payload != "" {
		err := json.Unmarshal([]byte(cmd.Payload), &term)
		if err != nil {
			return "", fmt.Errorf("Could not decode %s, error: %s", cmd.Payload, err)
		}
	}
	termID := cait.URIToID(term.URI)
	vocabularyID := cait.URIToVocabularyID(term.URI)
	if vocabularyID == 0 {
		return "", fmt.Errorf(`Can't determine vocabulary ID from uri, e.g. {"uri":"/vocabularies/1/terms"} or {"uri":"/vocabularies/1/terms/2"}, %s`, cmd.Payload)
	}
	switch cmd.Action {
	case "create":
		response, err := api.CreateTerm(vocabularyID, term)
		if err != nil {
			return "", err
		}
		if response.Status != "Created" {
			return "", fmt.Errorf("Create term status %s, %s", term.URI, response)
		}
		src, err := json.Marshal(response)
		if err != nil {
			return "", err
		}
		return string(src), nil
	case "list":
		//FIXME: calculate the vocabulary ID
		if termID == 0 {
			var ids []int
			ids, err := api.ListTermIDs(vocabularyID)
			if err != nil {
				return "", fmt.Errorf(`{"error": %q}`, err)
			}
			src, err := json.Marshal(ids)
			if err != nil {
				return "", fmt.Errorf(`{"error": "Cannot JSON encode %s %s"}`, cmd.Payload, err)
			}
			return string(src), nil
		}
		term, err := api.GetTerm(vocabularyID, termID)
		if err != nil {
			return "", fmt.Errorf(`{"error": %q}`, err)
		}
		src, err := json.Marshal(term)
		if err != nil {
			return "", fmt.Errorf(`{"error": "Cannot find %s %s"}`, cmd.Payload, err)
		}
		return string(src), nil
	case "update":
		responseMsg, err := api.UpdateTerm(term)
		if err != nil {
			return "", err
		}
		src, err := json.Marshal(responseMsg)
		return string(src), err
	case "delete":
		//FIXME: calculate the vocabulary ID
		term, err := api.GetTerm(vocabularyID, termID)
		if err != nil {
			return "", err
		}
		responseMsg, err := api.DeleteTerm(term)
		if err != nil {
			return "", err
		}
		src, err := json.Marshal(responseMsg)
		return string(src), err
	case "export":
		err := api.ExportTerms()
		if err != nil {
			return "", fmt.Errorf("Exporting /terms, %s", err)
		}
		return `{"status": "ok"}`, nil
	}
	return "", fmt.Errorf("action %s not implemented for %s", cmd.Action, cmd.Subject)
}
示例#2
0
func runResourceCmd(api *cait.ArchivesSpaceAPI, cmd *command) (string, error) {
	if err := api.Login(); err != nil {
		return "", err
	}
	obj := new(cait.Resource)
	if cmd.Payload != "" {
		err := json.Unmarshal([]byte(cmd.Payload), &obj)
		if err != nil {
			return "", fmt.Errorf("Could not decode %s, error: %s", cmd.Payload, err)
		}
	}
	objID := cait.URIToID(obj.URI)
	repoID := cait.URIToRepoID(obj.URI)
	if repoID == 0 {
		return "", fmt.Errorf(`Can't determine repository ID from uri, e.g. {"uri":"/repositories/2/resources"} or {"uri":"/repositories/2/resources/3"}`)
	}
	switch cmd.Action {
	case "create":
		response, err := api.CreateResource(repoID, obj)
		if err != nil {
			return "", fmt.Errorf("Create resource fialed %s, %s", obj.URI, err)
		}
		if response.Status != "Created" {
			return "", fmt.Errorf("Create resource status %s, %s", obj.URI, response)
		}
		src, err := json.Marshal(response)
		if err != nil {
			return "", fmt.Errorf("Create resource response %s, %s", obj.URI, err)
		}
		return string(src), nil
	case "list":
		if objID == 0 {
			objs, err := api.ListResources(repoID)
			if err != nil {
				return "", fmt.Errorf(`{"error": %q, "uri": "/repositories/%d/resources"}`, err, repoID)
			}
			src, err := json.Marshal(objs)
			if err != nil {
				return "", fmt.Errorf(`{"error": "Cannot JSON encode %s %s"}`, cmd.Payload, err)
			}
			return string(src), nil
		}
		obj, err := api.GetResource(repoID, objID)
		if err != nil {
			return "", fmt.Errorf(`{"error": %q}`, err)
		}
		src, err := json.Marshal(obj)
		if err != nil {
			return "", fmt.Errorf(`{"error": "Cannot find %s %s"}`, cmd.Payload, err)
		}
		return string(src), nil
	case "update":
		responseMsg, err := api.UpdateResource(obj)
		if err != nil {
			return "", err
		}
		src, err := json.Marshal(responseMsg)
		return string(src), err
	case "delete":
		obj, err := api.GetResource(repoID, objID)
		if err != nil {
			return "", err
		}
		responseMsg, err := api.DeleteResource(obj)
		if err != nil {
			return "", err
		}
		src, err := json.Marshal(responseMsg)
		return string(src), err
	case "export":
		err := api.ExportResources(repoID)
		if err != nil {
			return "", fmt.Errorf("Exporting repositories/%d/resources, %s", repoID, err)
		}
		return `{"status": "ok"}`, nil
	}
	return "", fmt.Errorf("runResourceCMd() action %s not implemented for %s", cmd.Action, cmd.Subject)
}
示例#3
0
func runVocabularyCmd(api *cait.ArchivesSpaceAPI, cmd *command) (string, error) {
	if err := api.Login(); err != nil {
		return "", err
	}
	vocabulary := new(cait.Vocabulary)
	if cmd.Payload != "" {
		err := json.Unmarshal([]byte(cmd.Payload), &vocabulary)
		if err != nil {
			return "", fmt.Errorf("Could not decode %s, error: %s", cmd.Payload, err)
		}
	}
	vocabularyID := cait.URIToID(vocabulary.URI)
	switch cmd.Action {
	case "create":
		response, err := api.CreateVocabulary(vocabulary)
		if err != nil {
			return "", err
		}
		if response.Status != "Created" {
			return "", fmt.Errorf("Create vocabulary status %s, %s", vocabulary.URI, response)
		}
		src, err := json.Marshal(response)
		if err != nil {
			return "", err
		}
		return string(src), nil
	case "list":
		if vocabularyID == 0 {
			var ids []int
			ids, err := api.ListVocabularies()
			if err != nil {
				return "", fmt.Errorf(`{"error": %q}`, err)
			}
			src, err := json.Marshal(ids)
			if err != nil {
				return "", fmt.Errorf(`{"error": "Cannot JSON encode %s %s"}`, cmd.Payload, err)
			}
			return string(src), nil
		}
		vocabulary, err := api.GetVocabulary(vocabularyID)
		if err != nil {
			return "", fmt.Errorf(`{"error": %q}`, err)
		}
		src, err := json.Marshal(vocabulary)
		if err != nil {
			return "", fmt.Errorf(`{"error": "Cannot find %s %s"}`, cmd.Payload, err)
		}
		return string(src), nil
	case "update":
		responseMsg, err := api.UpdateVocabulary(vocabulary)
		if err != nil {
			return "", err
		}
		src, err := json.Marshal(responseMsg)
		return string(src), err
	case "delete":
		vocabulary, err := api.GetVocabulary(vocabularyID)
		if err != nil {
			return "", err
		}
		responseMsg, err := api.DeleteVocabulary(vocabulary)
		if err != nil {
			return "", err
		}
		src, err := json.Marshal(responseMsg)
		return string(src), err
	case "export":
		err := api.ExportVocabularies()
		if err != nil {
			return "", fmt.Errorf("Exporting /vocabularies, %s", err)
		}
		return `{"status": "ok"}`, nil
	}
	return "", fmt.Errorf("action %s not implemented for %s", cmd.Action, cmd.Subject)
}
示例#4
0
func runLocationCmd(api *cait.ArchivesSpaceAPI, cmd *command) (string, error) {
	if err := api.Login(); err != nil {
		return "", err
	}
	location := new(cait.Location)
	if cmd.Payload != "" {
		err := json.Unmarshal([]byte(cmd.Payload), &location)
		if err != nil {
			return "", fmt.Errorf("Could not decode %s, error: %s", cmd.Payload, err)
		}
	}
	locationID := cait.URIToID(location.URI)
	switch cmd.Action {
	case "create":
		response, err := api.CreateLocation(location)
		if err != nil {
			return "", err
		}
		if response.Status != "Created" {
			return "", fmt.Errorf("Create location status %s, %s", location.URI, response)
		}
		src, err := json.Marshal(response)
		if err != nil {
			return "", err
		}
		return string(src), nil
	case "list":
		if locationID == 0 {
			locations, err := api.ListLocations()
			if err != nil {
				return "", fmt.Errorf(`{"error": %q}`, err)
			}
			src, err := json.Marshal(locations)
			if err != nil {
				return "", fmt.Errorf(`{"error": "Cannot JSON encode %s %s"}`, cmd.Payload, err)
			}
			return string(src), nil
		}
		location, err := api.GetLocation(locationID)
		if err != nil {
			return "", fmt.Errorf(`{"error": %q}`, err)
		}
		src, err := json.Marshal(location)
		if err != nil {
			return "", fmt.Errorf(`{"error": "Cannot find %s %s"}`, cmd.Payload, err)
		}
		return string(src), nil
	case "update":
		responseMsg, err := api.UpdateLocation(location)
		if err != nil {
			return "", err
		}
		src, err := json.Marshal(responseMsg)
		return string(src), err
	case "delete":
		location, err := api.GetLocation(locationID)
		if err != nil {
			return "", err
		}
		responseMsg, err := api.DeleteLocation(location)
		if err != nil {
			return "", err
		}
		src, err := json.Marshal(responseMsg)
		return string(src), err
	case "export":
		err := api.ExportLocations()
		if err != nil {
			return "", fmt.Errorf("Exporting /locations, %s", err)
		}
		return `{"status": "ok"}`, nil
	}
	return "", fmt.Errorf("action %s not implemented for %s", cmd.Action, cmd.Subject)
}
示例#5
0
func runAccessionCmd(api *cait.ArchivesSpaceAPI, cmd *command) (string, error) {
	if err := api.Login(); err != nil {
		return "", err
	}
	// Repo ID is passed as a JSON object
	accession := new(cait.Accession)
	if cmd.Payload != "" {
		err := json.Unmarshal([]byte(cmd.Payload), &accession)
		if err != nil {
			return "", fmt.Errorf("Could not decode %s, error: %s", cmd.Payload, err)
		}
	}
	accessionID := cait.URIToID(accession.URI)
	repoID := cait.URIToRepoID(accession.URI)
	if repoID == 0 {
		return "", fmt.Errorf(`Accession commands require a uri in the JSON payload, e.g. {"uri":"/repositories/2/accessions"} or {"uri":"/repositories/2/accecssions/333"}`)
	}
	switch cmd.Action {
	case "create":
		response, err := api.CreateAccession(repoID, accession)
		if err != nil {
			return "", err
		}
		if response.Status != "Created" {
			return "", fmt.Errorf("Create accession status %s, %s", accession.URI, response)
		}
		src, err := json.Marshal(response)
		if err != nil {
			return "", err
		}
		return string(src), nil
	case "list":
		if accessionID == 0 {
			accessions, err := api.ListAccessions(repoID)
			if err != nil {
				return "", fmt.Errorf(`{"uri": "/repositories/%d/accessions","error": %q}`, repoID, err)
			}
			src, err := json.Marshal(accessions)
			if err != nil {
				return "", fmt.Errorf(`{"error": "Cannot JSON encode %s %s"}`, cmd.Payload, err)
			}
			return string(src), nil
		}
		accession, err := api.GetAccession(repoID, accessionID)
		if err != nil {
			return "", fmt.Errorf(`{"error": %q}`, err)
		}
		src, err := json.Marshal(accession)
		if err != nil {
			return "", fmt.Errorf(`{"error": "Cannot find %s %s"}`, cmd.Payload, err)
		}
		return string(src), nil
	case "update":
		responseMsg, err := api.UpdateAccession(accession)
		if err != nil {
			return "", err
		}
		src, err := json.Marshal(responseMsg)
		return string(src), err
	case "delete":
		accession, err := api.GetAccession(repoID, accessionID)
		if err != nil {
			return "", err
		}
		responseMsg, err := api.DeleteAccession(accession)
		if err != nil {
			return "", err
		}
		src, err := json.Marshal(responseMsg)
		return string(src), err
	case "export":
		err := api.ExportAccessions(repoID)
		if err != nil {
			return "", fmt.Errorf("Exporting repositories/%d/accessions, %s", repoID, err)
		}
		return `{"status": "ok"}`, nil
	}
	return "", fmt.Errorf("action %s not implemented for %s", cmd.Action, cmd.Subject)
}
示例#6
0
func runAgentCmd(api *cait.ArchivesSpaceAPI, cmd *command) (string, error) {
	if err := api.Login(); err != nil {
		return "", err
	}
	//Agent Type Payload as JSON encoded objects
	agent := new(cait.Agent)
	if cmd.Payload != "" {
		err := json.Unmarshal([]byte(cmd.Payload), &agent)
		if err != nil {
			return "", fmt.Errorf("Could not decode cmd.Payload %s, error: %s", cmd.Payload, err)
		}
	}
	agentID := cait.URIToID(agent.URI)
	p := strings.Split(agent.URI, "/")
	if len(p) < 3 {
		return "", fmt.Errorf(`Agent commands require a uri in the JSON payload, e.g. {"uri":"/agents/people"} or {"uri":"/agents/poeple/3"}, %s`, cmd.Payload)
	} else if len(p) == 3 {
		agent.URI = ""
	}
	aType := p[2]
	switch cmd.Action {
	case "create":
		response, err := api.CreateAgent(aType, agent)
		if err != nil {
			return "", err
		}
		if response.Status != "Created" {
			return "", fmt.Errorf("Create agent status %s, %s", agent.URI, response)
		}
		src, err := json.Marshal(response)
		if err != nil {
			return "", err
		}
		return string(src), nil
	case "list":
		if agentID == 0 {
			agents, err := api.ListAgents(aType)
			if err != nil {
				return "", fmt.Errorf(`{"error": %q}`, err)
			}
			src, err := json.Marshal(agents)
			if err != nil {
				return "", fmt.Errorf(`{"error": "Cannot JSON encode %s %s"}`, cmd.Payload, err)
			}
			return string(src), nil
		}
		agent, err := api.GetAgent(aType, agentID)
		if err != nil {
			return "", fmt.Errorf(`{"error": %q}`, err)
		}
		src, err := json.Marshal(agent)
		if err != nil {
			return "", fmt.Errorf(`{"error": "Cannot find %s %s"}`, cmd.Payload, err)
		}
		return string(src), nil
	case "update":
		responseMsg, err := api.UpdateAgent(agent)
		if err != nil {
			return "", err
		}
		src, err := json.Marshal(responseMsg)
		return string(src), err
	case "delete":
		agent, err := api.GetAgent(aType, agentID)
		if err != nil {
			return "", err
		}
		responseMsg, err := api.DeleteAgent(agent)
		if err != nil {
			return "", err
		}
		src, err := json.Marshal(responseMsg)
		return string(src), err
	case "export":
		err := api.ExportAgents(aType)
		if err != nil {
			return "", fmt.Errorf("Exporting /agents/%s, %s", aType, err)
		}
		return `{"status": "ok"}`, nil
	}
	return "", fmt.Errorf("action %s not implemented for %s", cmd.Action, cmd.Subject)
}
示例#7
0
func runRepoCmd(api *cait.ArchivesSpaceAPI, cmd *command) (string, error) {
	if err := api.Login(); err != nil {
		return "", err
	}
	repoID := 0
	repo := new(cait.Repository)
	if cmd.Payload != "" {
		err := json.Unmarshal([]byte(cmd.Payload), repo)
		if err != nil {
			return "", fmt.Errorf("Problem unmashalling JSON repository request, %s", err)
		}
		repoID = cait.URIToID(repo.URI)
	}
	switch cmd.Action {
	case "create":
		response, err := api.CreateRepository(repo)
		if err != nil {
			return "", err
		}
		if response.Status != "Created" {
			return "", fmt.Errorf("Create repo status %s, %s", repo.URI, response)
		}
		src, err := json.Marshal(response)
		if err != nil {
			return "", err
		}
		return string(src), nil
	case "list":
		if repoID == 0 {
			repos, err := api.ListRepositories()
			if err != nil {
				return "", fmt.Errorf(`{"error": %q}`, err)
			}
			src, err := json.Marshal(repos)
			if err != nil {
				return "", fmt.Errorf(`{"error": "Cannot JSON encode %s %s"}`, cmd.Payload, err)
			}
			return string(src), nil
		}
		repo, err := api.GetRepository(repoID)
		if err != nil {
			return "", fmt.Errorf(`{"error": %q}`, err)
		}
		src, err := json.Marshal(repo)
		if err != nil {
			return "", fmt.Errorf(`{"error": "Cannot find %s %s"}`, cmd.Payload, err)
		}
		return string(src), nil
	case "update":
		responseMsg, err := api.UpdateRepository(repo)
		if err != nil {
			return "", err
		}
		src, err := json.Marshal(responseMsg)
		return string(src), err
	case "delete":
		repo, err := api.GetRepository(repoID)
		if err != nil {
			return "", err
		}
		responseMsg, err := api.DeleteRepository(repo)
		if err != nil {
			return "", err
		}
		src, err := json.Marshal(responseMsg)
		return string(src), err
	case "export":
		err := api.ExportRepository(
			repoID,
			path.Join(api.Dataset, "repositories"),
			fmt.Sprintf("%d.json", repoID),
		)
		if err != nil {
			return "", fmt.Errorf("Exporting repositories, %s", err)
		}
		return `{"status": "ok"}`, nil
	}
	return "", fmt.Errorf("action %s not implemented for %s", cmd.Action, cmd.Subject)
}