Beispiel #1
0
//create a new templates
func CreateTemplate(c *gin.Context) {

	type TemplatePayload struct {
		Template_name  string    `json:"template_name" binding:"required"`
		Commit_message string    `json:"commit_message" binding:"required"`
		Files          []tf.File `json:"files"`
	}

	var payload TemplatePayload
	c.Bind(&payload)

	var template tf.Template

	if &payload.Template_name == nil {
		c.String(http.StatusOK, "Template name is not provided")
	} else if &payload.Commit_message == nil {
		c.String(http.StatusOK, "Commit message is not provided")
	} else {
		template.Template_hash = tf.GenerateHashString()
		template.Template_name = payload.Template_name
		template.Release_info = tf.ReleaseInfo{"NULL", ""}
		template.Commit_info = tf.CommitInfo{"nguyener", time.Now().String(), payload.Commit_message, ""}
		template.Files = payload.Files

		if err := template.CreateTemplate(); err != nil {
			c.String(http.StatusInternalServerError, fmt.Sprintf("Failed to create new template.  Error: %s", err))
		} else {
			c.String(http.StatusOK, "A new template has been created successfully")
		}
	}
}
Beispiel #2
0
// Get all commits (versions) of a specified template
//(inclues duplicates due to multple release role.  caller needs to process it properly)
func GetTemplateCommits(c *gin.Context) {
	template_name := c.Params.ByName("template_name")

	var template tf.Template
	templates := template.GetTemplates()
	var templates_by_name []tf.Template
	for _, t := range templates {
		if t.Template_name == template_name {
			templates_by_name = append(templates_by_name, t)
		}
	}

	c.JSON(http.StatusOK, templates_by_name)

}
Beispiel #3
0
func DeleteTemplateResource(c *gin.Context) {
	template_hash := c.Params.ByName("template_hash")
	resource_type := c.Params.ByName("file_or_release")
	resource_name := c.Params.ByName("resource_name")
	if resource_type != "files" && resource_type != "release_info" {
		c.String(http.StatusBadRequest, "Unsupport resource type")
	} else {
		var template tf.Template
		template.Template_hash = template_hash

		if resource_type == "release_info" {
			if err := template.DeleteTemplateRelease(resource_name); err != nil {
				c.String(http.StatusInternalServerError, fmt.Sprintf("Failed to delete template release.  Error %s", err))
			} else {
				c.String(http.StatusOK, "A release has been delete successfully")
			}
		} else {
			fmt.Println("Deleting file " + resource_name)

			if err := template.DeleteTemplateFile(resource_name); err != nil {
				c.String(http.StatusInternalServerError, fmt.Sprintf("Failed to delete template fil.  Error: %s", err))
			} else {
				c.String(http.StatusOK, "A template files has been deleted successfully")
			}

		}
	}
}
Beispiel #4
0
func CreateTemplateResource(c *gin.Context) {
	template_hash := c.Params.ByName("template_hash")
	resource_type := c.Params.ByName("file_or_release")

	//DEBUG
	fmt.Println(template_hash)
	fmt.Println(resource_type)

	if resource_type != "files" && resource_type != "release_info" {
		c.String(http.StatusBadRequest, "Unsupport method")
	} else {
		var template tf.Template
		template.Template_hash = template_hash

		if resource_type == "release_info" {
			var payload tf.ReleaseInfo
			c.Bind(&payload)
			if err := template.CreateTemplateRelease(payload); err != nil {
				c.String(http.StatusInternalServerError, fmt.Sprintf("Failed to created template release.  Error %s", err))
			} else {
				c.String(http.StatusOK, "A release has been created successfully")
			}
		} else {
			var payload tf.File
			c.Bind(&payload)

			if err := template.AddTemplateFile(payload); err != nil {
				c.String(http.StatusInternalServerError, fmt.Sprintf("Failed to add template file.  Error: %s", err))
			} else {
				c.String(http.StatusOK, "A template file has been add successfully")
			}

		}
	}
}
Beispiel #5
0
// Get all templates (versions and duplicates due to multiple release role)
func GetTemplates(c *gin.Context) {
	var template tf.Template

	c.JSON(http.StatusOK, template.GetTemplates())
}