Example #1
0
func PubItem(repo, item string, p ds.PubPara, args []string) (err error) {

	if p.Ch_itemname == "" {
		p.Ch_itemname = item
	}

	url := repo + "/" + item
	if len(p.Accesstype) == 0 {
		p.Accesstype = PRIVATE
	}
	p.Accesstype = strings.ToLower(p.Accesstype)
	if p.Accesstype != PRIVATE && p.Accesstype != PUBLIC {
		fmt.Println("Error : Invalid accesstype, e.g accesstype=public, private")
		return
	}
	if len(p.SupplyStyle) == 0 {
		p.SupplyStyle = BATCH
	}
	p.SupplyStyle = strings.ToLower(p.SupplyStyle)
	if p.SupplyStyle != BATCH && p.SupplyStyle != FLOW && p.SupplyStyle != API {
		fmt.Println("Error : Invalid supplystyle, e.g supplystyle=batch, flow, api")
		return
	}
	if len(p.Datapool) == 0 {
		fmt.Println("DataHub : Publishing dataitem requires a parameter \"--datapool=???\" .")
		return
	}
	jsonData, err := json.Marshal(p)
	if err != nil {
		fmt.Println("Error : Marshal pubdata error while publishing dateitem.")
		return err
	}

	resp, err := commToDaemon("POST", "/repositories/"+url, jsonData)
	if err != nil {
		fmt.Println("Error :", err)
		return err
	}
	defer resp.Body.Close()
	body, _ := ioutil.ReadAll(resp.Body)
	if resp.StatusCode == http.StatusOK {
		result := ds.Result{}
		err = json.Unmarshal(body, &result)
		if err != nil {
			fmt.Println("Error : Pub error.", err) //todo add http code
			return err
		} else {
			if result.Code == 0 {
				fmt.Println("DataHub : Successed in publishing.")
			} else {
				fmt.Printf("Error : %v\n", result.Msg)
			}
		}
	} else if resp.StatusCode == http.StatusUnauthorized {
		if err = Login(false, nil); err == nil {
			Pub(true, args)
		} else {
			fmt.Println(err)
		}
	} else {
		result := ds.Result{}
		err = json.Unmarshal(body, &result)
		if err != nil {
			fmt.Println("Error : Pub error.", err)
			return err
		}
		if result.Code == ServerErrResultCode1008 {
			fmt.Printf("Error : Dataitem '%s' already exists.\n", item)
		} else if result.Code == ServerErrResultCode4010 {
			fmt.Printf("Error : Datapool '%s' not found.\n", p.Datapool)
		} else if result.Code == ServerErrResultCode1011 {
			fmt.Println("Error : Only 50 items should be included within each repository.")
		} else {
			fmt.Println("Error :", result.Msg)
		}
	}
	//err = pubResp(url, jsonData, args)
	return err
}