<commit_before>package gads

import (
	"bytes"
	"encoding/xml"
	"flag"
	"fmt"
	"io/ioutil"
	"net/http"
	"net/url"
	"testing"
)

const (
	baseUrl = "https://adwords.google.com/api/adwords/cm/v201502"
	//		baseUrl   = "http://127.0.0.1:4444"
	reportUrl = "https://adwords.google.com/api/adwords/reportdownload/v201502"
	//	reportUrl = "http://127.0.0.1:4444"
)

type ServiceUrl struct {
	Url  string
	Name string
}

// exceptions
var (
	ERROR_NOT_YET_IMPLEMENTED = fmt.Errorf("Not yet implemented")
)

var (
	configJson = flag.String("config_json", "./config.json", "API credentials")

	// service urls
	adGroupAdServiceUrl             = ServiceUrl{baseUrl, "AdGroupAdService"}
	adGroupBidModifierServiceUrl    = ServiceUrl{baseUrl, "AdGroupBidModifierService"}
	adGroupCriterionServiceUrl      = ServiceUrl{baseUrl, "AdGroupCriterionService"}
	adGroupFeedServiceUrl           = ServiceUrl{baseUrl, "AdGroupFeedService"}
	adGroupServiceUrl               = ServiceUrl{baseUrl, "AdGroupService"}
	adParamServiceUrl               = ServiceUrl{baseUrl, "AdParamService"}
	adwordsUserListServiceUrl       = ServiceUrl{baseUrl, "AdwordsUserListService"}
	biddingStrategyServiceUrl       = ServiceUrl{baseUrl, "BiddingStrategyService"}
	budgetOrderServiceUrl           = ServiceUrl{baseUrl, "BudgetOrderService"}
	budgetServiceUrl                = ServiceUrl{baseUrl, "BudgetService"}
	campaignAdExtensionServiceUrl   = ServiceUrl{baseUrl, "CampaignAdExtensionService"}
	campaignCriterionServiceUrl     = ServiceUrl{baseUrl, "CampaignCriterionService"}
	campaignFeedServiceUrl          = ServiceUrl{baseUrl, "CampaignFeedService"}
	campaignServiceUrl              = ServiceUrl{baseUrl, "CampaignService"}
	campaignSharedSetServiceUrl     = ServiceUrl{baseUrl, "CampaignSharedSetService"}
	constantDataServiceUrl          = ServiceUrl{baseUrl, "ConstantDataService"}
	conversionTrackerServiceUrl     = ServiceUrl{baseUrl, "ConversionTrackerService"}
	customerFeedServiceUrl          = ServiceUrl{baseUrl, "CustomerFeedService"}
	customerServiceUrl              = ServiceUrl{baseUrl, "CustomerService"}
	customerSyncServiceUrl          = ServiceUrl{baseUrl, "CustomerSyncService"}
	dataServiceUrl                  = ServiceUrl{baseUrl, "DataService"}
	experimentServiceUrl            = ServiceUrl{baseUrl, "ExperimentService"}
	feedItemServiceUrl              = ServiceUrl{baseUrl, "FeedItemService"}
	feedMappingServiceUrl           = ServiceUrl{baseUrl, "FeedMappingService"}
	feedServiceUrl                  = ServiceUrl{baseUrl, "FeedService"}
	geoLocationServiceUrl           = ServiceUrl{baseUrl, "GeoLocationService"}
	labelServiceUrl                 = ServiceUrl{baseUrl, "LabelService"}
	locationCriterionServiceUrl     = ServiceUrl{baseUrl, "LocationCriterionService"}
	managedCustomerServiceUrl       = ServiceUrl{baseUrl, "ManagedCustomerService"}
	mediaServiceUrl                 = ServiceUrl{baseUrl, "MediaService"}
	mutateJobServiceUrl             = ServiceUrl{baseUrl, "Mutate_JOB_Service"}
	offlineConversionFeedServiceUrl = ServiceUrl{baseUrl, "OfflineConversionFeedService"}
	reportDefinitionServiceUrl      = ServiceUrl{baseUrl, "ReportDefinitionService"}
	sharedCriterionServiceUrl       = ServiceUrl{baseUrl, "SharedCriterionService"}
	sharedSetServiceUrl             = ServiceUrl{baseUrl, "SharedSetService"}
	targetingIdeaServiceUrl         = ServiceUrl{baseUrl, "TargetingIdeaService"}
	trafficEstimatorServiceUrl      = ServiceUrl{baseUrl, "TrafficEstimatorService"}
)

func (s ServiceUrl) String() string {
	return s.Url + "/" + s.Name
}

type Auth struct {
	CustomerId     string
	DeveloperToken string
	UserAgent      string
	Testing        *testing.T   `json:"-"`
	Client         *http.Client `json:"-"`
}

//
// Selector structs
//
type DateRange struct {
	Min string `xml:"min"`
	Max string `xml:"max"`
}

type Predicate struct {
	Field    string   `xml:"field"`
	Operator string   `xml:"operator"`
	Values   []string `xml:"values"`
}

type OrderBy struct {
	Field     string `xml:"field"`
	SortOrder string `xml:"sortOrder"`
}

type Paging struct {
	Offset int64 `xml:"startIndex"`
	Limit  int64 `xml:"numberResults"`
}

type Selector struct {
	XMLName    xml.Name
	Fields     []string    `xml:"fields"`
	Predicates []Predicate `xml:"predicates"`
	DateRange  *DateRange  `xml:"dateRange,omitempty"`
	Ordering   []OrderBy   `xml:"ordering"`
	Paging     *Paging     `xml:"paging,omitempty"`
}

// error parsers
func selectorError() (err error) {
	return err
}

func (a *Auth) downloadReportRequest(body interface{}) (respBody []byte, err error) {
	reqBody, err := xml.MarshalIndent(body, "  ", "  ")

	if err != nil {
		return []byte{}, err
	}

	form := url.Values{}
	form.Add("__rdxml", string(reqBody))

	req, err := http.NewRequest("POST", reportUrl, bytes.NewBufferString(form.Encode()))
	req.Header.Add("User-Agent", a.UserAgent)
	req.Header.Add("clientCustomerId", a.CustomerId)
	req.Header.Add("developerToken", a.DeveloperToken)
	req.Header.Add("Accept", "*/*")
	req.Header.Add("Accept-Encoding", "gzip")
	req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
	contentLength := fmt.Sprintf("%d", len(reqBody))
	req.Header.Add("Content-length", contentLength)

	if a.Testing != nil {
		a.Testing.Logf("request ->\n%s\n%#v\n%s\n", req.URL.String(), req.Header, string(reqBody))
	}
	resp, err := a.Client.Do(req)
	if err != nil {
		return []byte{}, err
	}

	respBody, err = ioutil.ReadAll(resp.Body)
	if a.Testing != nil {
		a.Testing.Logf("respBody ->\n%s\n%s\n", string(respBody), resp.Status)
	}

	if resp.StatusCode == 400 || resp.StatusCode == 401 || resp.StatusCode == 403 || resp.StatusCode == 405 || resp.StatusCode == 500 {
		reportDownloadError := ReportDownloadError{}
		fmt.Printf("unknown error ->\n%s\n", string(respBody))
		err = xml.Unmarshal(respBody, &reportDownloadError)
		if err != nil {
			return respBody, fmt.Errorf("%v \nRespone:%s", err.Error(), respBody)
		}
		return respBody, reportDownloadError
	}

	return respBody, nil
}

func (a *Auth) request(serviceUrl ServiceUrl, action string, body interface{}) (respBody []byte, err error) {
	type devToken struct {
		XMLName xml.Name
	}
	type soapReqHeader struct {
		XMLName          xml.Name
		UserAgent        string `xml:"userAgent"`
		DeveloperToken   string `xml:"developerToken"`
		ClientCustomerId string `xml:"clientCustomerId"`
	}

	type soapReqBody struct {
		Body interface{}
	}

	type soapReqEnvelope struct {
		XMLName xml.Name
		Header  soapReqHeader `xml:"Header>RequestHeader"`
		Body    soapReqBody   `xml:"http://schemas.xmlsoap.org/soap/envelope/ Body"`
	}

	reqBody, err := xml.MarshalIndent(
		soapReqEnvelope{
			XMLName: xml.Name{"http://schemas.xmlsoap.org/soap/envelope/", "Envelope"},
			Header: soapReqHeader{
				XMLName:          xml.Name{serviceUrl.Url, "RequestHeader"},
				UserAgent:        a.UserAgent,
				DeveloperToken:   a.DeveloperToken,
				ClientCustomerId: a.CustomerId,
			},
			Body: soapReqBody{body},
		},
		"  ", "  ")

	if err != nil {
		return []byte{}, err
	}

	req, err := http.NewRequest("POST", serviceUrl.String(), bytes.NewReader(reqBody))
	req.Header.Add("Accept", "text/xml")
	req.Header.Add("Accept", "multipart/*")
	req.Header.Add("Content-Type", "text/xml;charset=UTF-8")
	contentLength := fmt.Sprintf("%d", len(reqBody))
	req.Header.Add("Content-length", contentLength)
	req.Header.Add("SOAPAction", action)
	if a.Testing != nil {
		a.Testing.Logf("request ->\n%s\n%#v\n%s\n", req.URL.String(), req.Header, string(reqBody))
	}
	resp, err := a.Client.Do(req)
	if err != nil {
		return []byte{}, err
	}

	respBody, err = ioutil.ReadAll(resp.Body)
	if a.Testing != nil {
		a.Testing.Logf("respBody ->\n%s\n%s\n", string(respBody), resp.Status)
	}

	type soapRespHeader struct {
		RequestId    string `xml:"requestId"`
		ServiceName  string `xml:"serviceName"`
		MethodName   string `xml:"methodName"`
		Operations   int64  `xml:"operations"`
		ResponseTime int64  `xml:"responseTime"`
	}

	type soapRespBody struct {
		Response []byte `xml:",innerxml"`
	}

	soapResp := struct {
		XMLName xml.Name       `xml:"http://schemas.xmlsoap.org/soap/envelope/ Envelope"`
		Header  soapRespHeader `xml:"Header>RequestHeader"`
		Body    soapRespBody   `xml:"http://schemas.xmlsoap.org/soap/envelope/ Body"`
	}{}

	err = xml.Unmarshal([]byte(respBody), &soapResp)
	if err != nil {
		return respBody, err
	}
	if resp.StatusCode == 400 || resp.StatusCode == 401 || resp.StatusCode == 403 || resp.StatusCode == 405 || resp.StatusCode == 500 {
		fault := Fault{}
		fmt.Printf("unknown error ->\n%s\n", string(soapResp.Body.Response))
		err = xml.Unmarshal(soapResp.Body.Response, &fault)
		if err != nil {
			return respBody, fmt.Errorf("%v \nRespone:%s", err.Error(), soapResp.Body.Response)
		}

		return soapResp.Body.Response, &fault.Errors
	}

	return soapResp.Body.Response, err
}
<commit_msg>Updated url for new version<commit_after>package gads

import (
	"bytes"
	"encoding/xml"
	"flag"
	"fmt"
	"io/ioutil"
	"net/http"
	"net/url"
	"testing"
)

const (
	baseUrl = "https://adwords.google.com/api/adwords/cm/v201509"
	//		baseUrl   = "http://127.0.0.1:4444"
	reportUrl = "https://adwords.google.com/api/adwords/reportdownload/v201509"
	//	reportUrl = "http://127.0.0.1:4444"
)

type ServiceUrl struct {
	Url  string
	Name string
}

// exceptions
var (
	ERROR_NOT_YET_IMPLEMENTED = fmt.Errorf("Not yet implemented")
)

var (
	configJson = flag.String("config_json", "./config.json", "API credentials")

	// service urls
	adGroupAdServiceUrl             = ServiceUrl{baseUrl, "AdGroupAdService"}
	adGroupBidModifierServiceUrl    = ServiceUrl{baseUrl, "AdGroupBidModifierService"}
	adGroupCriterionServiceUrl      = ServiceUrl{baseUrl, "AdGroupCriterionService"}
	adGroupFeedServiceUrl           = ServiceUrl{baseUrl, "AdGroupFeedService"}
	adGroupServiceUrl               = ServiceUrl{baseUrl, "AdGroupService"}
	adParamServiceUrl               = ServiceUrl{baseUrl, "AdParamService"}
	adwordsUserListServiceUrl       = ServiceUrl{baseUrl, "AdwordsUserListService"}
	biddingStrategyServiceUrl       = ServiceUrl{baseUrl, "BiddingStrategyService"}
	budgetOrderServiceUrl           = ServiceUrl{baseUrl, "BudgetOrderService"}
	budgetServiceUrl                = ServiceUrl{baseUrl, "BudgetService"}
	campaignAdExtensionServiceUrl   = ServiceUrl{baseUrl, "CampaignAdExtensionService"}
	campaignCriterionServiceUrl     = ServiceUrl{baseUrl, "CampaignCriterionService"}
	campaignFeedServiceUrl          = ServiceUrl{baseUrl, "CampaignFeedService"}
	campaignServiceUrl              = ServiceUrl{baseUrl, "CampaignService"}
	campaignSharedSetServiceUrl     = ServiceUrl{baseUrl, "CampaignSharedSetService"}
	constantDataServiceUrl          = ServiceUrl{baseUrl, "ConstantDataService"}
	conversionTrackerServiceUrl     = ServiceUrl{baseUrl, "ConversionTrackerService"}
	customerFeedServiceUrl          = ServiceUrl{baseUrl, "CustomerFeedService"}
	customerServiceUrl              = ServiceUrl{baseUrl, "CustomerService"}
	customerSyncServiceUrl          = ServiceUrl{baseUrl, "CustomerSyncService"}
	dataServiceUrl                  = ServiceUrl{baseUrl, "DataService"}
	experimentServiceUrl            = ServiceUrl{baseUrl, "ExperimentService"}
	feedItemServiceUrl              = ServiceUrl{baseUrl, "FeedItemService"}
	feedMappingServiceUrl           = ServiceUrl{baseUrl, "FeedMappingService"}
	feedServiceUrl                  = ServiceUrl{baseUrl, "FeedService"}
	geoLocationServiceUrl           = ServiceUrl{baseUrl, "GeoLocationService"}
	labelServiceUrl                 = ServiceUrl{baseUrl, "LabelService"}
	locationCriterionServiceUrl     = ServiceUrl{baseUrl, "LocationCriterionService"}
	managedCustomerServiceUrl       = ServiceUrl{baseUrl, "ManagedCustomerService"}
	mediaServiceUrl                 = ServiceUrl{baseUrl, "MediaService"}
	mutateJobServiceUrl             = ServiceUrl{baseUrl, "Mutate_JOB_Service"}
	offlineConversionFeedServiceUrl = ServiceUrl{baseUrl, "OfflineConversionFeedService"}
	reportDefinitionServiceUrl      = ServiceUrl{baseUrl, "ReportDefinitionService"}
	sharedCriterionServiceUrl       = ServiceUrl{baseUrl, "SharedCriterionService"}
	sharedSetServiceUrl             = ServiceUrl{baseUrl, "SharedSetService"}
	targetingIdeaServiceUrl         = ServiceUrl{baseUrl, "TargetingIdeaService"}
	trafficEstimatorServiceUrl      = ServiceUrl{baseUrl, "TrafficEstimatorService"}
)

func (s ServiceUrl) String() string {
	return s.Url + "/" + s.Name
}

type Auth struct {
	CustomerId     string
	DeveloperToken string
	UserAgent      string
	Testing        *testing.T   `json:"-"`
	Client         *http.Client `json:"-"`
}

//
// Selector structs
//
type DateRange struct {
	Min string `xml:"min"`
	Max string `xml:"max"`
}

type Predicate struct {
	Field    string   `xml:"field"`
	Operator string   `xml:"operator"`
	Values   []string `xml:"values"`
}

type OrderBy struct {
	Field     string `xml:"field"`
	SortOrder string `xml:"sortOrder"`
}

type Paging struct {
	Offset int64 `xml:"startIndex"`
	Limit  int64 `xml:"numberResults"`
}

type Selector struct {
	XMLName    xml.Name
	Fields     []string    `xml:"fields"`
	Predicates []Predicate `xml:"predicates"`
	DateRange  *DateRange  `xml:"dateRange,omitempty"`
	Ordering   []OrderBy   `xml:"ordering"`
	Paging     *Paging     `xml:"paging,omitempty"`
}

// error parsers
func selectorError() (err error) {
	return err
}

func (a *Auth) downloadReportRequest(body interface{}) (respBody []byte, err error) {
	reqBody, err := xml.MarshalIndent(body, "  ", "  ")

	if err != nil {
		return []byte{}, err
	}

	form := url.Values{}
	form.Add("__rdxml", string(reqBody))

	req, err := http.NewRequest("POST", reportUrl, bytes.NewBufferString(form.Encode()))
	req.Header.Add("User-Agent", a.UserAgent)
	req.Header.Add("clientCustomerId", a.CustomerId)
	req.Header.Add("developerToken", a.DeveloperToken)
	req.Header.Add("Accept", "*/*")
	req.Header.Add("Accept-Encoding", "gzip")
	req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
	contentLength := fmt.Sprintf("%d", len(reqBody))
	req.Header.Add("Content-length", contentLength)

	if a.Testing != nil {
		a.Testing.Logf("request ->\n%s\n%#v\n%s\n", req.URL.String(), req.Header, string(reqBody))
	}
	resp, err := a.Client.Do(req)
	if err != nil {
		return []byte{}, err
	}

	respBody, err = ioutil.ReadAll(resp.Body)
	if a.Testing != nil {
		a.Testing.Logf("respBody ->\n%s\n%s\n", string(respBody), resp.Status)
	}

	if resp.StatusCode == 400 || resp.StatusCode == 401 || resp.StatusCode == 403 || resp.StatusCode == 405 || resp.StatusCode == 500 {
		reportDownloadError := ReportDownloadError{}
		fmt.Printf("unknown error ->\n%s\n", string(respBody))
		err = xml.Unmarshal(respBody, &reportDownloadError)
		if err != nil {
			return respBody, fmt.Errorf("%v \nRespone:%s", err.Error(), respBody)
		}
		return respBody, reportDownloadError
	}

	return respBody, nil
}

func (a *Auth) request(serviceUrl ServiceUrl, action string, body interface{}) (respBody []byte, err error) {
	type devToken struct {
		XMLName xml.Name
	}
	type soapReqHeader struct {
		XMLName          xml.Name
		UserAgent        string `xml:"userAgent"`
		DeveloperToken   string `xml:"developerToken"`
		ClientCustomerId string `xml:"clientCustomerId"`
	}

	type soapReqBody struct {
		Body interface{}
	}

	type soapReqEnvelope struct {
		XMLName xml.Name
		Header  soapReqHeader `xml:"Header>RequestHeader"`
		Body    soapReqBody   `xml:"http://schemas.xmlsoap.org/soap/envelope/ Body"`
	}

	reqBody, err := xml.MarshalIndent(
		soapReqEnvelope{
			XMLName: xml.Name{"http://schemas.xmlsoap.org/soap/envelope/", "Envelope"},
			Header: soapReqHeader{
				XMLName:          xml.Name{serviceUrl.Url, "RequestHeader"},
				UserAgent:        a.UserAgent,
				DeveloperToken:   a.DeveloperToken,
				ClientCustomerId: a.CustomerId,
			},
			Body: soapReqBody{body},
		},
		"  ", "  ")

	if err != nil {
		return []byte{}, err
	}

	req, err := http.NewRequest("POST", serviceUrl.String(), bytes.NewReader(reqBody))
	req.Header.Add("Accept", "text/xml")
	req.Header.Add("Accept", "multipart/*")
	req.Header.Add("Content-Type", "text/xml;charset=UTF-8")
	contentLength := fmt.Sprintf("%d", len(reqBody))
	req.Header.Add("Content-length", contentLength)
	req.Header.Add("SOAPAction", action)
	if a.Testing != nil {
		a.Testing.Logf("request ->\n%s\n%#v\n%s\n", req.URL.String(), req.Header, string(reqBody))
	}
	resp, err := a.Client.Do(req)
	if err != nil {
		return []byte{}, err
	}

	respBody, err = ioutil.ReadAll(resp.Body)
	if a.Testing != nil {
		a.Testing.Logf("respBody ->\n%s\n%s\n", string(respBody), resp.Status)
	}

	type soapRespHeader struct {
		RequestId    string `xml:"requestId"`
		ServiceName  string `xml:"serviceName"`
		MethodName   string `xml:"methodName"`
		Operations   int64  `xml:"operations"`
		ResponseTime int64  `xml:"responseTime"`
	}

	type soapRespBody struct {
		Response []byte `xml:",innerxml"`
	}

	soapResp := struct {
		XMLName xml.Name       `xml:"http://schemas.xmlsoap.org/soap/envelope/ Envelope"`
		Header  soapRespHeader `xml:"Header>RequestHeader"`
		Body    soapRespBody   `xml:"http://schemas.xmlsoap.org/soap/envelope/ Body"`
	}{}

	err = xml.Unmarshal([]byte(respBody), &soapResp)
	if err != nil {
		return respBody, err
	}
	if resp.StatusCode == 400 || resp.StatusCode == 401 || resp.StatusCode == 403 || resp.StatusCode == 405 || resp.StatusCode == 500 {
		fault := Fault{}
		fmt.Printf("unknown error ->\n%s\n", string(soapResp.Body.Response))
		err = xml.Unmarshal(soapResp.Body.Response, &fault)
		if err != nil {
			return respBody, fmt.Errorf("%v \nRespone:%s", err.Error(), soapResp.Body.Response)
		}

		return soapResp.Body.Response, &fault.Errors
	}

	return soapResp.Body.Response, err
}
<|endoftext|>


<commit_before>package main

import (
	"encoding/json"
	"fmt"
	"github.com/ansible-semaphore/semaphore/db"
	"github.com/ansible-semaphore/semaphore/db/sql"
	"github.com/ansible-semaphore/semaphore/util"
	"github.com/snikch/goodman/transaction"
	"math/rand"
	"os"
	"strconv"
	"time"
)

var tablesShouldBeTruncated = [...]string{
	"access_key",
	"event",
	"user__token",
	"project",
	"task__output",
	"task",
	"session",
	"project__environment",
	"project__inventory",
	"project__repository",
	"project__template",
	"project__schedule",
	"project__user",
	"user",
	"project__view",
}

// Test Runner User
func addTestRunnerUser() {
	uid := getUUID()
	testRunnerUser = &db.User{
		Username: "ITU-" + uid,
		Name:     "ITU-" + uid,
		Email:    uid + "@semaphore.test",
		Created:  db.GetParsedTime(time.Now()),
		Admin:    true,
	}

	dbConnect()
	defer store.Sql().Db.Close()

	truncateAll()

	if err := store.Sql().Insert(testRunnerUser); err != nil {
		panic(err)
	}
	addToken(adminToken, testRunnerUser.ID)
}

func truncateAll() {
	tx, err := store.Sql().Begin()
	if err != nil {
		panic(err)
	}

	_, err = tx.Exec("SET FOREIGN_KEY_CHECKS = 0")
	if err == nil {
		for _, tableName := range tablesShouldBeTruncated {
			tx.Exec("TRUNCATE TABLE " + tableName)
		}
		tx.Exec("SET FOREIGN_KEY_CHECKS = 1")
	}

	if err := tx.Commit(); err != nil {
		panic(err)
	}
}

func removeTestRunnerUser(transactions []*transaction.Transaction) {
	dbConnect()
	defer store.Sql().Db.Close()
	deleteToken(adminToken, testRunnerUser.ID)
	deleteObject(testRunnerUser)
}

// Parameter Substitution
func setupObjectsAndPaths(t *transaction.Transaction) {
	alterRequestPath(t)
	alterRequestBody(t)
}

// Object Lifecycle
func addUserProjectRelation(pid int, user int) {
	_, err := store.Sql().Exec("insert into project__user (project_id, user_id, `admin`) values (?, ?, 1)", pid, user)
	if err != nil {
		panic(err)
	}
}

func deleteUserProjectRelation(pid int, user int) {
	_, err := store.Sql().Exec("delete from project__user where project_id=? and user_id=?", strconv.Itoa(pid), strconv.Itoa(user))
	if err != nil {
		panic(err)
	}
}

func addAccessKey(pid *int) *db.AccessKey {
	uid := getUUID()
	secret := "5up3r53cr3t\n"
	key := db.AccessKey{
		Name:      "ITK-" + uid,
		Type:      "ssh",
		Secret:    &secret,
		ProjectID: pid,
	}
	if err := store.Sql().Insert(&key); err != nil {
		panic(err)
	}
	return &key
}

func addProject() *db.Project {
	uid := getUUID()
	project := db.Project{
		Name:    "ITP-" + uid,
		Created: time.Now(),
	}
	if err := store.Sql().Insert(&project); err != nil {
		panic(err)
	}
	return &project
}

func addUser() *db.User {
	uid := getUUID()
	user := db.User{
		Created:  time.Now(),
		Username: "ITU-" + uid,
		Email:    "test@semaphore." + uid,
	}
	if err := store.Sql().Insert(&user); err != nil {
		panic(err)
	}
	return &user
}

func addView() *db.View {
	view, err := store.CreateView(db.View{
		ProjectID: userProject.ID,
		Title:     "Test",
		Position:  1,
	})

	if err != nil {
		panic(err)
	}

	return &view
}

func addSchedule() *db.Schedule {
	schedule, err := store.CreateSchedule(db.Schedule{
		TemplateID: int(templateID),
		CronFormat: "* * * 1 *",
		ProjectID:  userProject.ID,
	})

	if err != nil {
		panic(err)
	}

	return &schedule
}

func addTask() *db.Task {
	t := db.Task{
		ProjectID:  userProject.ID,
		TemplateID: int(templateID),
		Status:     "testing",
		UserID:     &userPathTestUser.ID,
		Created:    db.GetParsedTime(time.Now()),
	}
	if err := store.Sql().Insert(&t); err != nil {
		fmt.Println("error during insertion of task:")
		if j, err := json.Marshal(t); err == nil {
			fmt.Println(string(j))
		} else {
			fmt.Println("can not stringify task object")
		}
		panic(err)
	}
	return &t
}

func deleteObject(i interface{}) {
	_, err := store.Sql().Delete(i)
	if err != nil {
		panic(err)
	}
}

// Token Handling
func addToken(tok string, user int) {
	token := db.APIToken{
		ID:      tok,
		Created: time.Now(),
		UserID:  user,
		Expired: false,
	}
	if err := store.Sql().Insert(&token); err != nil {
		panic(err)
	}
}

func deleteToken(tok string, user int) {
	token := db.APIToken{
		ID:     tok,
		UserID: user,
	}
	deleteObject(&token)
}

// HELPERS
var r *rand.Rand
var randSetup = false

func getUUID() string {
	if !randSetup {
		r = rand.New(rand.NewSource(time.Now().UnixNano()))
		randSetup = true
	}
	return randomString(8)
}
func randomString(strlen int) string {
	const chars = "abcdefghijklmnopqrstuvwxyz0123456789"
	result := ""
	for i := 0; i < strlen; i++ {
		index := r.Intn(len(chars))
		result += chars[index : index+1]
	}
	return result
}

func loadConfig() {
	cwd, _ := os.Getwd()
	file, _ := os.Open(cwd + "/.dredd/config.json")
	if err := json.NewDecoder(file).Decode(&util.Config); err != nil {
		fmt.Println("Could not decode configuration!")
		panic(err)
	}
}

var store sql.SqlDb

func dbConnect() {
	store = sql.SqlDb{}

	if err := store.Connect(); err != nil {
		panic(err)
	}
}

func stringInSlice(a string, list []string) (int, bool) {
	for k, b := range list {
		if b == a {
			return k, true
		}
	}
	return 0, false
}

func printError(err error) {
	if err != nil {
		//fmt.Println(err)
		panic(err)
	}
}
<commit_msg>test: use store<commit_after>package main

import (
	"encoding/json"
	"fmt"
	"github.com/ansible-semaphore/semaphore/db"
	"github.com/ansible-semaphore/semaphore/db/sql"
	"github.com/ansible-semaphore/semaphore/util"
	"github.com/snikch/goodman/transaction"
	"math/rand"
	"os"
	"strconv"
	"time"
)

var tablesShouldBeTruncated = [...]string{
	"access_key",
	"event",
	"user__token",
	"project",
	"task__output",
	"task",
	"session",
	"project__environment",
	"project__inventory",
	"project__repository",
	"project__template",
	"project__schedule",
	"project__user",
	"user",
	"project__view",
}

// Test Runner User
func addTestRunnerUser() {
	uid := getUUID()
	testRunnerUser = &db.User{
		Username: "ITU-" + uid,
		Name:     "ITU-" + uid,
		Email:    uid + "@semaphore.test",
		Created:  db.GetParsedTime(time.Now()),
		Admin:    true,
	}

	dbConnect()
	defer store.Sql().Db.Close()

	truncateAll()

	if err := store.Sql().Insert(testRunnerUser); err != nil {
		panic(err)
	}
	addToken(adminToken, testRunnerUser.ID)
}

func truncateAll() {
	tx, err := store.Sql().Begin()
	if err != nil {
		panic(err)
	}

	_, err = tx.Exec("SET FOREIGN_KEY_CHECKS = 0")
	if err == nil {
		for _, tableName := range tablesShouldBeTruncated {
			tx.Exec("TRUNCATE TABLE " + tableName)
		}
		tx.Exec("SET FOREIGN_KEY_CHECKS = 1")
	}

	if err := tx.Commit(); err != nil {
		panic(err)
	}
}

func removeTestRunnerUser(transactions []*transaction.Transaction) {
	dbConnect()
	defer store.Sql().Db.Close()
	deleteToken(adminToken, testRunnerUser.ID)
	deleteObject(testRunnerUser)
}

// Parameter Substitution
func setupObjectsAndPaths(t *transaction.Transaction) {
	alterRequestPath(t)
	alterRequestBody(t)
}

// Object Lifecycle
func addUserProjectRelation(pid int, user int) {
	_, err := store.CreateProjectUser(db.ProjectUser{
		ProjectID: pid,
		UserID:    user,
		Admin:     true,
	})
	if err != nil {
		panic(err)
	}
}

func deleteUserProjectRelation(pid int, user int) {
	_, err := store.Sql().Exec("delete from project__user where project_id=? and user_id=?", strconv.Itoa(pid), strconv.Itoa(user))
	if err != nil {
		panic(err)
	}
}

func addAccessKey(pid *int) *db.AccessKey {
	uid := getUUID()
	secret := "5up3r53cr3t\n"
	key := db.AccessKey{
		Name:      "ITK-" + uid,
		Type:      "ssh",
		Secret:    &secret,
		ProjectID: pid,
	}
	if err := store.Sql().Insert(&key); err != nil {
		panic(err)
	}
	return &key
}

func addProject() *db.Project {
	uid := getUUID()
	project := db.Project{
		Name:    "ITP-" + uid,
		Created: time.Now(),
	}
	if err := store.Sql().Insert(&project); err != nil {
		panic(err)
	}
	return &project
}

func addUser() *db.User {
	uid := getUUID()
	user := db.User{
		Created:  time.Now(),
		Username: "ITU-" + uid,
		Email:    "test@semaphore." + uid,
	}
	if err := store.Sql().Insert(&user); err != nil {
		panic(err)
	}
	return &user
}

func addView() *db.View {
	view, err := store.CreateView(db.View{
		ProjectID: userProject.ID,
		Title:     "Test",
		Position:  1,
	})

	if err != nil {
		panic(err)
	}

	return &view
}

func addSchedule() *db.Schedule {
	schedule, err := store.CreateSchedule(db.Schedule{
		TemplateID: int(templateID),
		CronFormat: "* * * 1 *",
		ProjectID:  userProject.ID,
	})

	if err != nil {
		panic(err)
	}

	return &schedule
}

func addTask() *db.Task {
	t := db.Task{
		ProjectID:  userProject.ID,
		TemplateID: int(templateID),
		Status:     "testing",
		UserID:     &userPathTestUser.ID,
		Created:    db.GetParsedTime(time.Now()),
	}
	if err := store.Sql().Insert(&t); err != nil {
		fmt.Println("error during insertion of task:")
		if j, err := json.Marshal(t); err == nil {
			fmt.Println(string(j))
		} else {
			fmt.Println("can not stringify task object")
		}
		panic(err)
	}
	return &t
}

func deleteObject(i interface{}) {
	_, err := store.Sql().Delete(i)
	if err != nil {
		panic(err)
	}
}

// Token Handling
func addToken(tok string, user int) {
	token := db.APIToken{
		ID:      tok,
		Created: time.Now(),
		UserID:  user,
		Expired: false,
	}
	if err := store.Sql().Insert(&token); err != nil {
		panic(err)
	}
}

func deleteToken(tok string, user int) {
	token := db.APIToken{
		ID:     tok,
		UserID: user,
	}
	deleteObject(&token)
}

// HELPERS
var r *rand.Rand
var randSetup = false

func getUUID() string {
	if !randSetup {
		r = rand.New(rand.NewSource(time.Now().UnixNano()))
		randSetup = true
	}
	return randomString(8)
}
func randomString(strlen int) string {
	const chars = "abcdefghijklmnopqrstuvwxyz0123456789"
	result := ""
	for i := 0; i < strlen; i++ {
		index := r.Intn(len(chars))
		result += chars[index : index+1]
	}
	return result
}

func loadConfig() {
	cwd, _ := os.Getwd()
	file, _ := os.Open(cwd + "/.dredd/config.json")
	if err := json.NewDecoder(file).Decode(&util.Config); err != nil {
		fmt.Println("Could not decode configuration!")
		panic(err)
	}
}

var store sql.SqlDb

func dbConnect() {
	store = sql.SqlDb{}

	if err := store.Connect(); err != nil {
		panic(err)
	}
}

func stringInSlice(a string, list []string) (int, bool) {
	for k, b := range list {
		if b == a {
			return k, true
		}
	}
	return 0, false
}

func printError(err error) {
	if err != nil {
		//fmt.Println(err)
		panic(err)
	}
}
<|endoftext|>


<commit_before>/*

Copyright (c) 2010 Andrea Fazzi

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

*/

package prettytest

import (
	"testing"
	"os"
	"io/ioutil"
)

type testSuite struct { Suite }
type beforeAfterSuite struct { Suite }
type bddFormatterSuite struct { Suite }

var (
	basicAssertions testSuite
	beforeAfter beforeAfterSuite
	bddFormatter bddFormatterSuite
	state int = 0
	beforeState = 0
	afterState = 0
	beforeAllState int = 0
	afterAllState int = 0
)

func (suite *testSuite) testTrueFalse() {
	suite.True(true)
	suite.False(false)
}

func (suite *testSuite) testEqualNotEqual() {
	suite.Equal("foo", "foo")
	suite.NotEqual("foo", "bar")
}

func (suite *testSuite) testNil() {
	suite.Nil(nil)
}

func (suite *testSuite) testNotNil() {
	suite.NotNil([]byte{1,2,3})
}

func (suite *testSuite) testPath() {
	ioutil.WriteFile("testfile", nil, 0600)
	suite.Path("testfile")
	// suite.Path("foo")
	// suite.True(suite.Failed())
}

func (suite *testSuite) testPending() {
	suite.Pending()
}

func (suite *testSuite) after() {
	os.Remove("testfile")
}

func (suite *beforeAfterSuite) before() {
	state += 2
	beforeState++
}

func (suite *beforeAfterSuite) after() {
	state--
	afterState--
}

func (suite *beforeAfterSuite) beforeAll() {
	state = 0
	beforeAllState++
}

func (suite *beforeAfterSuite) afterAll() {
	state = 0
	afterAllState--
}

func (suite *beforeAfterSuite) testSetup_1() {
	suite.Equal(2, state)
}

func (suite *beforeAfterSuite) testSetup_2() {
	suite.Equal(3, state)
}

func TestBasicAssertions(t *testing.T) {
	Run(
		t,
		&basicAssertions,
		&beforeAfter,
	)
	if beforeAllState != 1 {
		t.Errorf("beforeAllState should be 1 after all tests but was %d\n", beforeAllState)
	}
	if afterAllState != -1 {
		t.Errorf("afterAllState should be -1 after all tests but was %d\n", afterAllState)
	}
}

func (suite *bddFormatterSuite) should_use_green_on_passing_examples() {
	suite.True(true)
}

func (suite *bddFormatterSuite) should_use_yellow_on_pending_examples() {
	suite.Pending()
}

func TestBDDStyleSpecs(t *testing.T) {
	RunWithFormatter(
		t,
		&BDDFormatter{Description: "BDD Formatter"},
		&bddFormatter,
	)
}
<commit_msg>Clean up prettytest_test.go<commit_after>/*

Copyright (c) 2010 Andrea Fazzi

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

*/

package prettytest

import (
	"testing"
	"os"
	"io/ioutil"
)

var state, beforeState, afterState, beforeAllState, afterAllState int

type testSuite struct { Suite }
type beforeAfterSuite struct { Suite }
type bddFormatterSuite struct { Suite }

func (suite *testSuite) testTrueFalse() {
	suite.True(true)
	suite.False(false)
}

func (suite *testSuite) testEqualNotEqual() {
	suite.Equal("foo", "foo")
	suite.NotEqual("foo", "bar")
}

func (suite *testSuite) testNil() {
	suite.Nil(nil)
}

func (suite *testSuite) testNotNil() {
	suite.NotNil([]byte{1,2,3})
}

func (suite *testSuite) testPath() {
	ioutil.WriteFile("testfile", nil, 0600)
	suite.Path("testfile")
	// suite.Path("foo")
	// suite.True(suite.Failed())
}

func (suite *testSuite) testPending() {
	suite.Pending()
}

func (suite *testSuite) after() {
	os.Remove("testfile")
}

func (suite *beforeAfterSuite) before() {
	state += 2
	beforeState++
}

func (suite *beforeAfterSuite) after() {
	state--
	afterState--
}

func (suite *beforeAfterSuite) beforeAll() {
	state = 0
	beforeAllState++
}

func (suite *beforeAfterSuite) afterAll() {
	state = 0
	afterAllState--
}

func (suite *beforeAfterSuite) testSetup_1() {
	suite.Equal(2, state)
}

func (suite *beforeAfterSuite) testSetup_2() {
	suite.Equal(3, state)
}

func TestPrettyTest(t *testing.T) {
	Run(
		t,
		new(testSuite),
		new(beforeAfterSuite),
	)
	if beforeAllState != 1 {
		t.Errorf("beforeAllState should be 1 after all tests but was %d\n", beforeAllState)
	}
	if afterAllState != -1 {
		t.Errorf("afterAllState should be -1 after all tests but was %d\n", afterAllState)
	}
}

func (suite *bddFormatterSuite) should_use_green_on_passing_examples() {
	suite.True(true)
}

func (suite *bddFormatterSuite) should_use_yellow_on_pending_examples() {
	suite.Pending()
}

func TestBDDStyleSpecs(t *testing.T) {
	RunWithFormatter(
		t,
		&BDDFormatter{Description: "BDD Formatter"},
		new(bddFormatterSuite),
	)
}
<|endoftext|>


<commit_before>package process

import (
    "bytes"
    "io"
    "fmt"
    "os/exec"
    "github.com/johnny-morrice/pipeline"
    lib "github.com/johnny-morrice/godelbrot/libgodelbrot"
)

// Config creates a new Info, given the args, and sends it to stdout.
func Config(stdout io.Writer, stderr io.Writer, args []string) error {
    config := configbrot(args)
    return runPipeCmd(config, &bytes.Buffer{}, stdout, stderr)
}

// Render sends a new fractal image to the passed stdout pipe, corresponding to the Info
// serialized in stdin.
func Render(stdin io.Reader, stdout io.Writer, stderr io.Writer) error {
    render := renderbrot()
    return runPipeCmd(render, stdin, stdout, stderr)
}

// ConfigRender sends a new fractal image to the passed stdout pipe, corresponding to configbrot's
// processing of the args slice.
func ConfigRender(stdout io.Writer, stderr io.Writer, args []string) error {
    config := configbrot(args)
    render := renderbrot()

    pl := pipeline.New(&bytes.Buffer{}, stdout, stderr)
    pl.Chain(config, render)
    return pl.Exec()
}

// Zoom magnifies a section of the Info read from stdin, sending it to stdout.
func Zoom(stdin io.Reader, stdout io.Writer, stderr io.Writer, args[]string) error {
    zoom := zoombrot(args)
    return runPipeCmd(zoom, stdin, stdout, stderr)
}

// Zoom reads Info from stdin, and sends a fractal to stdout, returning the magnified Info,
// serialized as an io.Reader.
func ZoomRender(stdin io.Reader, stdout io.Writer, stderr io.Writer, args []string) (io.Reader, error) {
    zoomBuff := &bytes.Buffer{}
    zoomerr := Zoom(stdin, zoomBuff, stderr, args)
    if zoomerr != nil {
        return nil, zoomerr
    }

    outbuff := &bytes.Buffer{}
    rendin := io.TeeReader(zoomBuff, outbuff)

    err := Render(rendin, stdout, stderr)

    return outbuff, err
}

func ZoomArgs(target lib.ZoomTarget) []string {
    formal := []string{
        "frames",
        "incprec",
        "reconf",
        "xmax",
        "xmin",
        "ymax",
        "ymin",
    }
    actual := []string{
        fmt.Sprint(target.Frames),
        fmt.Sprint(target.UpPrec),
        fmt.Sprint(target.UpPrec),
        fmt.Sprint(target.Xmin),
        fmt.Sprint(target.Xmax),
        fmt.Sprint(target.Ymin),
        fmt.Sprint(target.Ymax),
    }

    opts := make([]string, len(formal))
    for i, fm := range formal {
        opts[i] = fmt.Sprintf("-%v=%v", fm, actual[i])
    }

    return opts
}

func zoombrot(args []string) *exec.Cmd {
    return exec.Command("zoombrot", args...)
}

func configbrot(args []string) *exec.Cmd {
    return exec.Command("configbrot", args...)
}

func renderbrot() *exec.Cmd {
    return exec.Command("renderbrot")
}

func runPipeCmd(cmd *exec.Cmd, stdin io.Reader, stdout, stderr io.Writer) error {
    cmd.Stdin = stdin
    cmd.Stdout = stdout
    cmd.Stderr = stderr
    return cmd.Run()
}<commit_msg>Fix external zoom command parameters<commit_after>package process

import (
    "bytes"
    "io"
    "fmt"
    "os/exec"
    "github.com/johnny-morrice/pipeline"
    lib "github.com/johnny-morrice/godelbrot/libgodelbrot"
)

// Config creates a new Info, given the args, and sends it to stdout.
func Config(stdout io.Writer, stderr io.Writer, args []string) error {
    config := configbrot(args)
    return runPipeCmd(config, &bytes.Buffer{}, stdout, stderr)
}

// Render sends a new fractal image to the passed stdout pipe, corresponding to the Info
// serialized in stdin.
func Render(stdin io.Reader, stdout io.Writer, stderr io.Writer) error {
    render := renderbrot()
    return runPipeCmd(render, stdin, stdout, stderr)
}

// ConfigRender sends a new fractal image to the passed stdout pipe, corresponding to configbrot's
// processing of the args slice.
func ConfigRender(stdout io.Writer, stderr io.Writer, args []string) error {
    config := configbrot(args)
    render := renderbrot()

    pl := pipeline.New(&bytes.Buffer{}, stdout, stderr)
    pl.Chain(config, render)
    return pl.Exec()
}

// Zoom magnifies a section of the Info read from stdin, sending it to stdout.
func Zoom(stdin io.Reader, stdout io.Writer, stderr io.Writer, args[]string) error {
    zoom := zoombrot(args)
    return runPipeCmd(zoom, stdin, stdout, stderr)
}

// Zoom reads Info from stdin, and sends a fractal to stdout, returning the magnified Info,
// serialized as an io.Reader.
func ZoomRender(stdin io.Reader, stdout io.Writer, stderr io.Writer, args []string) (io.Reader, error) {
    zoomBuff := &bytes.Buffer{}
    zoomerr := Zoom(stdin, zoomBuff, stderr, args)
    if zoomerr != nil {
        return nil, zoomerr
    }

    outbuff := &bytes.Buffer{}
    rendin := io.TeeReader(zoomBuff, outbuff)

    err := Render(rendin, stdout, stderr)

    return outbuff, err
}

func ZoomArgs(target lib.ZoomTarget) []string {
    formal := []string{
        "frames",
        "incprec",
        "reconf",
        "xmin",
        "xmax",
        "ymin",
        "ymax",
    }
    actual := []string{
        fmt.Sprint(target.Frames),
        fmt.Sprint(target.UpPrec),
        fmt.Sprint(target.UpPrec),
        fmt.Sprint(target.Xmin),
        fmt.Sprint(target.Xmax),
        fmt.Sprint(target.Ymin),
        fmt.Sprint(target.Ymax),
    }

    opts := make([]string, len(formal))
    for i, fm := range formal {
        opts[i] = fmt.Sprintf("-%v=%v", fm, actual[i])
    }

    return opts
}

func zoombrot(args []string) *exec.Cmd {
    return exec.Command("zoombrot", args...)
}

func configbrot(args []string) *exec.Cmd {
    return exec.Command("configbrot", args...)
}

func renderbrot() *exec.Cmd {
    return exec.Command("renderbrot")
}

func runPipeCmd(cmd *exec.Cmd, stdin io.Reader, stdout, stderr io.Writer) error {
    cmd.Stdin = stdin
    cmd.Stdout = stdout
    cmd.Stderr = stderr
    return cmd.Run()
}<|endoftext|>


<commit_before>package serviceprincipals

import (
	"context"
	"errors"
	"log"
	"net/http"
	"time"

	"github.com/hashicorp/terraform-provider-azuread/internal/utils"
	"github.com/manicminer/hamilton/msgraph"

	"github.com/hashicorp/terraform-provider-azuread/internal/tf"
	"github.com/manicminer/hamilton/odata"

	"github.com/hashicorp/terraform-provider-azuread/internal/clients"

	"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
	"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
	"github.com/hashicorp/terraform-provider-azuread/internal/validate"
)

func servicePrincipalDelegatedPermissionGrantResource() *schema.Resource {
	return &schema.Resource{
		CreateContext: servicePrincipalDelegatedPermissionGrantResourceCreate,
		UpdateContext: servicePrincipalDelegatedPermissionGrantResourceUpdate,
		ReadContext:   servicePrincipalDelegatedPermissionGrantResourceRead,
		DeleteContext: servicePrincipalDelegatedPermissionGrantResourceDelete,

		Timeouts: &schema.ResourceTimeout{
			Create: schema.DefaultTimeout(5 * time.Minute),
			Read:   schema.DefaultTimeout(5 * time.Minute),
			Update: schema.DefaultTimeout(5 * time.Minute),
			Delete: schema.DefaultTimeout(5 * time.Minute),
		},

		Schema: map[string]*schema.Schema{
			"claim_values": {
				Description: "A set of claim values for delegated permission scopes which should be included in access tokens for the resource",
				Type:        schema.TypeSet,
				Required:    true,
				MinItems:    1,
				Elem: &schema.Schema{
					Type:             schema.TypeString,
					ValidateDiagFunc: validate.NoEmptyStrings,
				},
			},

			"resource_service_principal_object_id": {
				Description:      "The object ID of the service principal representing the resource to be accessed",
				Type:             schema.TypeString,
				Required:         true,
				ForceNew:         true,
				ValidateDiagFunc: validate.UUID,
			},

			"service_principal_object_id": {
				Description:      "The object ID of the service principal for which this delegated permission grant should be created",
				Type:             schema.TypeString,
				Required:         true,
				ForceNew:         true,
				ValidateDiagFunc: validate.UUID,
			},

			"user_object_id": {
				Description:      "The object ID of the user on behalf of whom the service principal is authorized to access the resource",
				Type:             schema.TypeString,
				Optional:         true,
				ForceNew:         true,
				ValidateDiagFunc: validate.UUID,
			},
		},
	}
}

func servicePrincipalDelegatedPermissionGrantResourceCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
	client := meta.(*clients.Client).ServicePrincipals.DelegatedPermissionGrantsClient
	servicePrincipalsClient := meta.(*clients.Client).ServicePrincipals.ServicePrincipalsClient

	servicePrincipalId := d.Get("service_principal_object_id").(string)
	resourceId := d.Get("resource_service_principal_object_id").(string)

	if _, status, err := servicePrincipalsClient.Get(ctx, servicePrincipalId, odata.Query{}); err != nil {
		if status == http.StatusNotFound {
			return tf.ErrorDiagPathF(err, "principal_object_id", "Service principal with object ID %q was not found)", servicePrincipalId)
		}
		return tf.ErrorDiagF(err, "Could not retrieve service principal with object ID %q", servicePrincipalId)
	}

	if _, status, err := servicePrincipalsClient.Get(ctx, resourceId, odata.Query{}); err != nil {
		if status == http.StatusNotFound {
			return tf.ErrorDiagPathF(err, "principal_object_id", "Service principal not found for resource (Object ID: %q)", resourceId)
		}
		return tf.ErrorDiagF(err, "Could not retrieve service principal for resource (Object ID: %q)", resourceId)
	}

	properties := msgraph.DelegatedPermissionGrant{
		ClientId:   utils.String(servicePrincipalId),
		ResourceId: utils.String(resourceId),
		Scopes:     tf.ExpandStringSlicePtr(d.Get("claim_values").(*schema.Set).List()),
	}

	if v, ok := d.GetOk("user_object_id"); ok && v.(string) != "" {
		properties.PrincipalId = utils.String(v.(string))
		properties.ConsentType = utils.String(msgraph.DelegatedPermissionGrantConsentTypePrincipal)
	} else {
		properties.ConsentType = utils.String(msgraph.DelegatedPermissionGrantConsentTypeAllPrincipals)
	}

	delegatedPermissionGrant, _, err := client.Create(ctx, properties)
	if err != nil {
		return tf.ErrorDiagF(err, "Could not create delegated permission grant")
	}

	if delegatedPermissionGrant.Id == nil || *delegatedPermissionGrant.Id == "" {
		return tf.ErrorDiagF(errors.New("ID returned for delegated permission grant is nil"), "Bad API response")
	}

	d.SetId(*delegatedPermissionGrant.Id)

	return servicePrincipalDelegatedPermissionGrantResourceRead(ctx, d, meta)
}

func servicePrincipalDelegatedPermissionGrantResourceUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
	client := meta.(*clients.Client).ServicePrincipals.DelegatedPermissionGrantsClient

	properties := msgraph.DelegatedPermissionGrant{
		Id:     utils.String(d.Id()),
		Scopes: tf.ExpandStringSlicePtr(d.Get("claim_values").(*schema.Set).List()),
	}

	if _, err := client.Update(ctx, properties); err != nil {
		return tf.ErrorDiagF(err, "Could not update delegated permission grant")
	}

	return servicePrincipalDelegatedPermissionGrantResourceRead(ctx, d, meta)
}

func servicePrincipalDelegatedPermissionGrantResourceRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
	client := meta.(*clients.Client).ServicePrincipals.DelegatedPermissionGrantsClient

	delegatedPermissionGrant, status, err := client.Get(ctx, d.Id(), odata.Query{})
	if err != nil {
		if status == http.StatusNoContent {
			log.Printf("[DEBUG] Delegated Permission Grant with ID %q was not found - removing from state", d.Id())
			d.SetId("")
			return nil
		}
		return tf.ErrorDiagPathF(err, "id", "Retrieving Delegated Permission Grant with ID %q", d.Id())
	}

	tf.Set(d, "claim_values", delegatedPermissionGrant.Scopes)
	tf.Set(d, "resource_service_principal_object_id", delegatedPermissionGrant.ResourceId)
	tf.Set(d, "service_principal_object_id", delegatedPermissionGrant.ClientId)
	tf.Set(d, "user_object_id", delegatedPermissionGrant.PrincipalId)

	return nil
}

func servicePrincipalDelegatedPermissionGrantResourceDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
	client := meta.(*clients.Client).ServicePrincipals.DelegatedPermissionGrantsClient

	id := d.Id()

	if status, err := client.Delete(ctx, id); err != nil {
		return tf.ErrorDiagPathF(err, "id", "Deleting delegated permission grant with ID %q, got status %d", id, status)
	}

	return nil
}
<commit_msg>goimports<commit_after>package serviceprincipals

import (
	"context"
	"errors"
	"log"
	"net/http"
	"time"

	"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
	"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
	"github.com/manicminer/hamilton/msgraph"
	"github.com/manicminer/hamilton/odata"

	"github.com/hashicorp/terraform-provider-azuread/internal/clients"
	"github.com/hashicorp/terraform-provider-azuread/internal/tf"
	"github.com/hashicorp/terraform-provider-azuread/internal/utils"
	"github.com/hashicorp/terraform-provider-azuread/internal/validate"
)

func servicePrincipalDelegatedPermissionGrantResource() *schema.Resource {
	return &schema.Resource{
		CreateContext: servicePrincipalDelegatedPermissionGrantResourceCreate,
		UpdateContext: servicePrincipalDelegatedPermissionGrantResourceUpdate,
		ReadContext:   servicePrincipalDelegatedPermissionGrantResourceRead,
		DeleteContext: servicePrincipalDelegatedPermissionGrantResourceDelete,

		Timeouts: &schema.ResourceTimeout{
			Create: schema.DefaultTimeout(5 * time.Minute),
			Read:   schema.DefaultTimeout(5 * time.Minute),
			Update: schema.DefaultTimeout(5 * time.Minute),
			Delete: schema.DefaultTimeout(5 * time.Minute),
		},

		Schema: map[string]*schema.Schema{
			"claim_values": {
				Description: "A set of claim values for delegated permission scopes which should be included in access tokens for the resource",
				Type:        schema.TypeSet,
				Required:    true,
				MinItems:    1,
				Elem: &schema.Schema{
					Type:             schema.TypeString,
					ValidateDiagFunc: validate.NoEmptyStrings,
				},
			},

			"resource_service_principal_object_id": {
				Description:      "The object ID of the service principal representing the resource to be accessed",
				Type:             schema.TypeString,
				Required:         true,
				ForceNew:         true,
				ValidateDiagFunc: validate.UUID,
			},

			"service_principal_object_id": {
				Description:      "The object ID of the service principal for which this delegated permission grant should be created",
				Type:             schema.TypeString,
				Required:         true,
				ForceNew:         true,
				ValidateDiagFunc: validate.UUID,
			},

			"user_object_id": {
				Description:      "The object ID of the user on behalf of whom the service principal is authorized to access the resource",
				Type:             schema.TypeString,
				Optional:         true,
				ForceNew:         true,
				ValidateDiagFunc: validate.UUID,
			},
		},
	}
}

func servicePrincipalDelegatedPermissionGrantResourceCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
	client := meta.(*clients.Client).ServicePrincipals.DelegatedPermissionGrantsClient
	servicePrincipalsClient := meta.(*clients.Client).ServicePrincipals.ServicePrincipalsClient

	servicePrincipalId := d.Get("service_principal_object_id").(string)
	resourceId := d.Get("resource_service_principal_object_id").(string)

	if _, status, err := servicePrincipalsClient.Get(ctx, servicePrincipalId, odata.Query{}); err != nil {
		if status == http.StatusNotFound {
			return tf.ErrorDiagPathF(err, "principal_object_id", "Service principal with object ID %q was not found)", servicePrincipalId)
		}
		return tf.ErrorDiagF(err, "Could not retrieve service principal with object ID %q", servicePrincipalId)
	}

	if _, status, err := servicePrincipalsClient.Get(ctx, resourceId, odata.Query{}); err != nil {
		if status == http.StatusNotFound {
			return tf.ErrorDiagPathF(err, "principal_object_id", "Service principal not found for resource (Object ID: %q)", resourceId)
		}
		return tf.ErrorDiagF(err, "Could not retrieve service principal for resource (Object ID: %q)", resourceId)
	}

	properties := msgraph.DelegatedPermissionGrant{
		ClientId:   utils.String(servicePrincipalId),
		ResourceId: utils.String(resourceId),
		Scopes:     tf.ExpandStringSlicePtr(d.Get("claim_values").(*schema.Set).List()),
	}

	if v, ok := d.GetOk("user_object_id"); ok && v.(string) != "" {
		properties.PrincipalId = utils.String(v.(string))
		properties.ConsentType = utils.String(msgraph.DelegatedPermissionGrantConsentTypePrincipal)
	} else {
		properties.ConsentType = utils.String(msgraph.DelegatedPermissionGrantConsentTypeAllPrincipals)
	}

	delegatedPermissionGrant, _, err := client.Create(ctx, properties)
	if err != nil {
		return tf.ErrorDiagF(err, "Could not create delegated permission grant")
	}

	if delegatedPermissionGrant.Id == nil || *delegatedPermissionGrant.Id == "" {
		return tf.ErrorDiagF(errors.New("ID returned for delegated permission grant is nil"), "Bad API response")
	}

	d.SetId(*delegatedPermissionGrant.Id)

	return servicePrincipalDelegatedPermissionGrantResourceRead(ctx, d, meta)
}

func servicePrincipalDelegatedPermissionGrantResourceUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
	client := meta.(*clients.Client).ServicePrincipals.DelegatedPermissionGrantsClient

	properties := msgraph.DelegatedPermissionGrant{
		Id:     utils.String(d.Id()),
		Scopes: tf.ExpandStringSlicePtr(d.Get("claim_values").(*schema.Set).List()),
	}

	if _, err := client.Update(ctx, properties); err != nil {
		return tf.ErrorDiagF(err, "Could not update delegated permission grant")
	}

	return servicePrincipalDelegatedPermissionGrantResourceRead(ctx, d, meta)
}

func servicePrincipalDelegatedPermissionGrantResourceRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
	client := meta.(*clients.Client).ServicePrincipals.DelegatedPermissionGrantsClient

	delegatedPermissionGrant, status, err := client.Get(ctx, d.Id(), odata.Query{})
	if err != nil {
		if status == http.StatusNoContent {
			log.Printf("[DEBUG] Delegated Permission Grant with ID %q was not found - removing from state", d.Id())
			d.SetId("")
			return nil
		}
		return tf.ErrorDiagPathF(err, "id", "Retrieving Delegated Permission Grant with ID %q", d.Id())
	}

	tf.Set(d, "claim_values", delegatedPermissionGrant.Scopes)
	tf.Set(d, "resource_service_principal_object_id", delegatedPermissionGrant.ResourceId)
	tf.Set(d, "service_principal_object_id", delegatedPermissionGrant.ClientId)
	tf.Set(d, "user_object_id", delegatedPermissionGrant.PrincipalId)

	return nil
}

func servicePrincipalDelegatedPermissionGrantResourceDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
	client := meta.(*clients.Client).ServicePrincipals.DelegatedPermissionGrantsClient

	id := d.Id()

	if status, err := client.Delete(ctx, id); err != nil {
		return tf.ErrorDiagPathF(err, "id", "Deleting delegated permission grant with ID %q, got status %d", id, status)
	}

	return nil
}
<|endoftext|>


<commit_before><commit_msg> fix select replication<commit_after><|endoftext|>


<commit_before>package lifecycle_test

import (
	"fmt"
	"net/http"

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

var _ = Describe("Garden", func() {

	var debugAddr string

	BeforeEach(func() {
		debugAddr = fmt.Sprintf("0.0.0.0:%d", 15000+GinkgoParallelNode())

	})

	It("started without --debugAddr flag", func() {
		client = startGarden()

		_, err := http.Get(fmt.Sprintf("http://%s/debug/pprof/?debug=1", debugAddr))
		Ω(err).Should(HaveOccurred())

	})

	It("started with --debugAddr flag", func() {
		client = startGarden("--debugAddr", debugAddr)

		_, err := http.Get(fmt.Sprintf("http://%s/debug/pprof/?debug=1", debugAddr))
		Ω(err).ShouldNot(HaveOccurred())

		_, err = http.Get(fmt.Sprintf("http://%s/log-level -X PUT -d debug", debugAddr))
		Ω(err).ShouldNot(HaveOccurred())

		_, err = http.Get(fmt.Sprintf("http://%s/log-level -X PUT -d info", debugAddr))
		Ω(err).ShouldNot(HaveOccurred())

		_, err = http.Get(fmt.Sprintf("http://%s/log-level -X PUT -d error", debugAddr))
		Ω(err).ShouldNot(HaveOccurred())

		_, err = http.Get(fmt.Sprintf("http://%s/log-level -X PUT -d fatal", debugAddr))
		Ω(err).ShouldNot(HaveOccurred())
	})

})
<commit_msg>Refactor flags_test<commit_after>package lifecycle_test

import (
	"fmt"
	"net/http"

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

var _ = Describe("Garden startup flags", func() {

	var debugAddr string

	BeforeEach(func() {
		debugAddr = fmt.Sprintf("0.0.0.0:%d", 15000+GinkgoParallelNode())
	})

	Context("when starting without the --debugAddr flag", func() {
		BeforeEach(func() {
			client = startGarden()
		})

		It("does not expose the pprof debug endpoint", func() {
			_, err := http.Get(fmt.Sprintf("http://%s/debug/pprof/?debug=1", debugAddr))
			Ω(err).Should(HaveOccurred())
		})

		It("does not expose the log level adjustment endpoint", func() {
			_, err := http.Get(fmt.Sprintf("http://%s/log-level -X PUT -d debug", debugAddr))
			Ω(err).Should(HaveOccurred())
		})
	})

	Context("when starting with the --debugAddr flag", func() {
		BeforeEach(func() {
			client = startGarden("--debugAddr", debugAddr)
		})

		It("exposes the pprof debug endpoint", func() {
			_, err := http.Get(fmt.Sprintf("http://%s/debug/pprof/?debug=1", debugAddr))
			Ω(err).ShouldNot(HaveOccurred())
		})

		It("exposes the log level adjustment endpoint", func() {
			_, err := http.Get(fmt.Sprintf("http://%s/log-level -X PUT -d debug", debugAddr))
			Ω(err).ShouldNot(HaveOccurred())

			_, err = http.Get(fmt.Sprintf("http://%s/log-level -X PUT -d info", debugAddr))
			Ω(err).ShouldNot(HaveOccurred())

			_, err = http.Get(fmt.Sprintf("http://%s/log-level -X PUT -d error", debugAddr))
			Ω(err).ShouldNot(HaveOccurred())

			_, err = http.Get(fmt.Sprintf("http://%s/log-level -X PUT -d fatal", debugAddr))
			Ω(err).ShouldNot(HaveOccurred())
		})
	})
})
<|endoftext|>


<commit_before>// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package frontend

import (
	"context"
	"errors"

	"golang.org/x/pkgsite/internal"
	"golang.org/x/pkgsite/internal/derrors"
	"golang.org/x/pkgsite/internal/log"
)

// LatestVersion returns the latest version of the package or module.
// The linkable form of the version is returned.
// It returns the empty string on error.
// It is intended to be used as an argument to middleware.LatestVersion.
func (s *Server) LatestVersion(ctx context.Context, packagePath, modulePath, pageType string) string {
	v, err := s.latestVersion(ctx, packagePath, modulePath, pageType)
	if err != nil {
		// We get NotFound errors from directories; they clutter the log.
		if !errors.Is(err, derrors.NotFound) {
			log.Errorf(ctx, "GetLatestVersion: %v", err)
		}
		return ""
	}
	return v
}

func (s *Server) latestVersion(ctx context.Context, packagePath, modulePath, pageType string) (_ string, err error) {
	defer derrors.Wrap(&err, "latestVersion(ctx, %q, %q)", modulePath, packagePath)

	var mi *internal.LegacyModuleInfo
	switch pageType {
	case "mod":
		mi, err = s.ds.LegacyGetModuleInfo(ctx, modulePath, internal.LatestVersion)
		if err != nil {
			return "", err
		}
	case "pkg":
		pkg, err := s.ds.LegacyGetPackage(ctx, packagePath, modulePath, internal.LatestVersion)
		if err != nil {
			return "", err
		}
		mi = &pkg.LegacyModuleInfo
	default:
		// For directories we don't have a well-defined latest version.
		return "", nil
	}
	return linkVersion(mi.Version, modulePath), nil
}
<commit_msg>internal/frontend: use GetPathInfo for latest version<commit_after>// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package frontend

import (
	"context"
	"errors"

	"golang.org/x/pkgsite/internal"
	"golang.org/x/pkgsite/internal/derrors"
	"golang.org/x/pkgsite/internal/experiment"
	"golang.org/x/pkgsite/internal/log"
)

// LatestVersion returns the latest version of the package or module.
// The linkable form of the version is returned.
// It returns the empty string on error.
// It is intended to be used as an argument to middleware.LatestVersion.
func (s *Server) LatestVersion(ctx context.Context, packagePath, modulePath, pageType string) string {
	v, err := s.latestVersion(ctx, packagePath, modulePath, pageType)
	if err != nil {
		// We get NotFound errors from directories; they clutter the log.
		if !errors.Is(err, derrors.NotFound) {
			log.Errorf(ctx, "GetLatestVersion: %v", err)
		}
		return ""
	}
	return v
}

// TODO(https://github.com/golang/go/issues/40107): this is currently tested in server_test.go, but
// we should add tests for this function.
func (s *Server) latestVersion(ctx context.Context, packagePath, modulePath, pageType string) (_ string, err error) {
	defer derrors.Wrap(&err, "latestVersion(ctx, %q, %q)", modulePath, packagePath)
	if experiment.IsActive(ctx, internal.ExperimentUsePathInfo) {
		fullPath := packagePath
		if pageType == "mod" {
			fullPath = modulePath
		}
		modulePath, version, _, err := s.ds.GetPathInfo(ctx, fullPath, modulePath, internal.LatestVersion)
		if err != nil {
			return "", err
		}
		return linkVersion(version, modulePath), nil
	}

	var mi *internal.LegacyModuleInfo
	switch pageType {
	case "mod":
		mi, err = s.ds.LegacyGetModuleInfo(ctx, modulePath, internal.LatestVersion)
		if err != nil {
			return "", err
		}
	case "pkg":
		pkg, err := s.ds.LegacyGetPackage(ctx, packagePath, modulePath, internal.LatestVersion)
		if err != nil {
			return "", err
		}
		mi = &pkg.LegacyModuleInfo
	default:
		// For directories we don't have a well-defined latest version.
		return "", nil
	}
	return linkVersion(mi.Version, modulePath), nil
}
<|endoftext|>


<commit_before>// Copyright 2022 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package gcimporter_test

import (
	"fmt"
	"go/ast"
	"go/parser"
	"go/token"
	"go/types"
	"testing"

	"golang.org/x/sync/errgroup"
	"golang.org/x/tools/go/packages"
	"golang.org/x/tools/internal/gcimporter"
	"golang.org/x/tools/internal/testenv"
)

// TestStd type-checks the standard library using shallow export data.
func TestShallowStd(t *testing.T) {
	if testing.Short() {
		t.Skip("skipping in short mode; too slow (https://golang.org/issue/14113)")
	}
	testenv.NeedsTool(t, "go")

	// Load import graph of the standard library.
	// (No parsing or type-checking.)
	cfg := &packages.Config{
		Mode:  packages.LoadImports,
		Tests: false,
	}
	pkgs, err := packages.Load(cfg, "std")
	if err != nil {
		t.Fatalf("load: %v", err)
	}
	if len(pkgs) < 200 {
		t.Fatalf("too few packages: %d", len(pkgs))
	}

	// Type check the packages in parallel postorder.
	done := make(map[*packages.Package]chan struct{})
	packages.Visit(pkgs, nil, func(p *packages.Package) {
		done[p] = make(chan struct{})
	})
	packages.Visit(pkgs, nil,
		func(pkg *packages.Package) {
			go func() {
				// Wait for all deps to be done.
				for _, imp := range pkg.Imports {
					<-done[imp]
				}
				typecheck(t, pkg)
				close(done[pkg])
			}()
		})
	for _, root := range pkgs {
		<-done[root]
	}
}

// typecheck reads, parses, and type-checks a package.
// It squirrels the export data in the the ppkg.ExportFile field.
func typecheck(t *testing.T, ppkg *packages.Package) {
	if ppkg.PkgPath == "unsafe" {
		return // unsafe is special
	}

	// Create a local FileSet just for this package.
	fset := token.NewFileSet()

	// Parse files in parallel.
	syntax := make([]*ast.File, len(ppkg.CompiledGoFiles))
	var group errgroup.Group
	for i, filename := range ppkg.CompiledGoFiles {
		i, filename := i, filename
		group.Go(func() error {
			f, err := parser.ParseFile(fset, filename, nil, parser.SkipObjectResolution)
			if err != nil {
				return err // e.g. missing file
			}
			syntax[i] = f
			return nil
		})
	}
	if err := group.Wait(); err != nil {
		t.Fatal(err)
	}
	// Inv: all files were successfully parsed.

	// Build map of dependencies by package path.
	// (We don't compute this mapping for the entire
	// packages graph because it is not globally consistent.)
	depsByPkgPath := make(map[string]*packages.Package)
	{
		var visit func(*packages.Package)
		visit = func(pkg *packages.Package) {
			if depsByPkgPath[pkg.PkgPath] == nil {
				depsByPkgPath[pkg.PkgPath] = pkg
				for path := range pkg.Imports {
					visit(pkg.Imports[path])
				}
			}
		}
		visit(ppkg)
	}

	// importer state
	var (
		insert    func(p *types.Package, name string)
		importMap = make(map[string]*types.Package) // keys are PackagePaths
	)

	loadFromExportData := func(imp *packages.Package) (*types.Package, error) {
		data := []byte(imp.ExportFile)
		return gcimporter.IImportShallow(fset, importMap, data, imp.PkgPath, insert)
	}
	insert = func(p *types.Package, name string) {
		imp, ok := depsByPkgPath[p.Path()]
		if !ok {
			t.Fatalf("can't find dependency: %q", p.Path())
		}
		imported, err := loadFromExportData(imp)
		if err != nil {
			t.Fatalf("unmarshal: %v", err)
		}
		obj := imported.Scope().Lookup(name)
		if obj == nil {
			t.Fatalf("lookup %q.%s failed", imported.Path(), name)
		}
		if imported != p {
			t.Fatalf("internal error: inconsistent packages")
		}
	}

	cfg := &types.Config{
		Error: func(e error) {
			t.Error(e)
		},
		Importer: importerFunc(func(importPath string) (*types.Package, error) {
			if importPath == "unsafe" {
				return types.Unsafe, nil // unsafe has no exportdata
			}
			imp, ok := ppkg.Imports[importPath]
			if !ok {
				return nil, fmt.Errorf("missing import %q", importPath)
			}
			return loadFromExportData(imp)
		}),
	}

	// Type-check the syntax trees.
	tpkg, _ := cfg.Check(ppkg.PkgPath, fset, syntax, nil)

	// Save the export data.
	data, err := gcimporter.IExportShallow(fset, tpkg)
	if err != nil {
		t.Fatalf("internal error marshalling export data: %v", err)
	}
	ppkg.ExportFile = string(data)
}
<commit_msg>internal/gcimporter: use nondeprecated go/packages mode bits<commit_after>// Copyright 2022 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package gcimporter_test

import (
	"fmt"
	"go/ast"
	"go/parser"
	"go/token"
	"go/types"
	"testing"

	"golang.org/x/sync/errgroup"
	"golang.org/x/tools/go/packages"
	"golang.org/x/tools/internal/gcimporter"
	"golang.org/x/tools/internal/testenv"
)

// TestStd type-checks the standard library using shallow export data.
func TestShallowStd(t *testing.T) {
	if testing.Short() {
		t.Skip("skipping in short mode; too slow (https://golang.org/issue/14113)")
	}
	testenv.NeedsTool(t, "go")

	// Load import graph of the standard library.
	// (No parsing or type-checking.)
	cfg := &packages.Config{
		Mode: packages.NeedImports |
			packages.NeedName |
			packages.NeedFiles | // see https://github.com/golang/go/issues/56632
			packages.NeedCompiledGoFiles,
		Tests: false,
	}
	pkgs, err := packages.Load(cfg, "std")
	if err != nil {
		t.Fatalf("load: %v", err)
	}
	if len(pkgs) < 200 {
		t.Fatalf("too few packages: %d", len(pkgs))
	}

	// Type check the packages in parallel postorder.
	done := make(map[*packages.Package]chan struct{})
	packages.Visit(pkgs, nil, func(p *packages.Package) {
		done[p] = make(chan struct{})
	})
	packages.Visit(pkgs, nil,
		func(pkg *packages.Package) {
			go func() {
				// Wait for all deps to be done.
				for _, imp := range pkg.Imports {
					<-done[imp]
				}
				typecheck(t, pkg)
				close(done[pkg])
			}()
		})
	for _, root := range pkgs {
		<-done[root]
	}
}

// typecheck reads, parses, and type-checks a package.
// It squirrels the export data in the the ppkg.ExportFile field.
func typecheck(t *testing.T, ppkg *packages.Package) {
	if ppkg.PkgPath == "unsafe" {
		return // unsafe is special
	}

	// Create a local FileSet just for this package.
	fset := token.NewFileSet()

	// Parse files in parallel.
	syntax := make([]*ast.File, len(ppkg.CompiledGoFiles))
	var group errgroup.Group
	for i, filename := range ppkg.CompiledGoFiles {
		i, filename := i, filename
		group.Go(func() error {
			f, err := parser.ParseFile(fset, filename, nil, parser.SkipObjectResolution)
			if err != nil {
				return err // e.g. missing file
			}
			syntax[i] = f
			return nil
		})
	}
	if err := group.Wait(); err != nil {
		t.Fatal(err)
	}
	// Inv: all files were successfully parsed.

	// Build map of dependencies by package path.
	// (We don't compute this mapping for the entire
	// packages graph because it is not globally consistent.)
	depsByPkgPath := make(map[string]*packages.Package)
	{
		var visit func(*packages.Package)
		visit = func(pkg *packages.Package) {
			if depsByPkgPath[pkg.PkgPath] == nil {
				depsByPkgPath[pkg.PkgPath] = pkg
				for path := range pkg.Imports {
					visit(pkg.Imports[path])
				}
			}
		}
		visit(ppkg)
	}

	// importer state
	var (
		insert    func(p *types.Package, name string)
		importMap = make(map[string]*types.Package) // keys are PackagePaths
	)
	loadFromExportData := func(imp *packages.Package) (*types.Package, error) {
		data := []byte(imp.ExportFile)
		return gcimporter.IImportShallow(fset, importMap, data, imp.PkgPath, insert)
	}
	insert = func(p *types.Package, name string) {
		imp, ok := depsByPkgPath[p.Path()]
		if !ok {
			t.Fatalf("can't find dependency: %q", p.Path())
		}
		imported, err := loadFromExportData(imp)
		if err != nil {
			t.Fatalf("unmarshal: %v", err)
		}
		if imported != p {
			t.Fatalf("internal error: inconsistent packages")
		}
		if obj := imported.Scope().Lookup(name); obj == nil {
			t.Fatalf("lookup %q.%s failed", imported.Path(), name)
		}
	}

	cfg := &types.Config{
		Error: func(e error) {
			t.Error(e)
		},
		Importer: importerFunc(func(importPath string) (*types.Package, error) {
			if importPath == "unsafe" {
				return types.Unsafe, nil // unsafe has no exportdata
			}
			imp, ok := ppkg.Imports[importPath]
			if !ok {
				return nil, fmt.Errorf("missing import %q", importPath)
			}
			return loadFromExportData(imp)
		}),
	}

	// Type-check the syntax trees.
	tpkg, _ := cfg.Check(ppkg.PkgPath, fset, syntax, nil)

	// Save the export data.
	data, err := gcimporter.IExportShallow(fset, tpkg)
	if err != nil {
		t.Fatalf("internal error marshalling export data: %v", err)
	}
	ppkg.ExportFile = string(data)
}
<|endoftext|>


<commit_before>package main

import (
	//"byte"
	//"crypto/x509"
	"bufio"
	"fmt"
	"os"
	"regexp"
)

func main() {
	//TODO: read commands
	//catapult user@server:port --keyfile  --password --passphrase

	//split strings with escape of \ for spaces in arguments
	r := regexp.MustCompile("(\\\\.|[^\\s])+")
	scanner := bufio.NewScanner(os.Stdin)
	for scanner.Scan() {
		input := scanner.Text()
		//TODO: read gets and puts commands here
		args := r.FindAllString(input, -1)
		switch cmd := args[0]; cmd {
		case "gets":
			//gets remotedir localdir otherdirs
			fmt.Println("Doing a gets command")
		case "puts":
			//puts localdir remotedir local_sent_dir
			fmt.Println("Doing a puts command")
		case "list":
			//list remotedir
			fmt.Println("Doing a list command")
		default:
			fmt.Println("Unknown command: %s", cmd)
		}
	}
}<commit_msg>parsing args<commit_after>package main

import (
	//"byte"
	//"crypto/x509"
	"bufio"
	"flag"
	"fmt"
	"os"
	"regexp"
	"strings"
	"golang.org/x/crypto/ssh"
)

//catapult [-keyfile=... [-passphrase=..] | -password=.. ] user@server:port

var client *ssh.Client
var password string
var passphrase string
var keyfile string

func usage() {
	fmt.Fprintf(os.Stderr,"catapult <flags> user@server:port\n")
	fmt.Fprintln(os.Stderr,"")
	fmt.Fprintln(os.Stderr,"Note: flags must come before connection string")
}

func init() {
	flag.StringVar(&password, "password", "", "password for sftp connection")
	flag.StringVar(&passphrase, "passphrase", "", "passphrase for keyfile")
	flag.StringVar(&keyfile, "keyfile", "", "keyfile path")
}

func main() {
	flag.Parse()

	if (flag.NArg() != 1) {
		usage()
		os.Exit(2)
	}

	connection := strings.SplitN(flag.Arg(0), "@", 2)
	if (len(connection) != 2) {
		usage()
		os.Exit(2)
	}
	username := connection[0]
	address := connection[1]

	config := &ssh.ClientConfig{
		User: username,
	}

	if keyfile != "" {
		config.Auth = []ssh.AuthMethod{ ssh.Password(password) }
	} else if password != "" {
		config.Auth = []ssh.AuthMethod{ ssh.Password(password) }
	} else {
		fmt.Fprintln(os.Stderr, "Need password or keyfile")
	}

	hasPort, err := regexp.MatchString(".+:\\d+", address)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(2)
	}
	if !hasPort {
		address = address + ":22"
	}

	fmt.Println(username, address)

	client, err = ssh.Dial("tcp", address, config)
	if err != nil {
		fmt.Fprintln(os.Stderr, "Failed to connect to server")
		fmt.Fprintln(os.Stderr, err)
		os.Exit(2)
	}

	//split strings with escape of \ for spaces in arguments
	r := regexp.MustCompile("(\\\\.|[^\\s])+")
	scanner := bufio.NewScanner(os.Stdin)
	for scanner.Scan() {
		input := scanner.Text()
		//TODO: read gets and puts commands here
		args := r.FindAllString(input, -1)
		switch cmd := args[0]; cmd {
		case "gets":
			//gets remotedir localdir otherdirs
			fmt.Println("Doing a gets command")
		case "puts":
			//puts localdir remotedir local_sent_dir
			fmt.Println("Doing a puts command")
		case "list":
			//list remotedir
			fmt.Println("Doing a list command")
		default:
			fmt.Println("Unknown command: %s", cmd)
		}
	}
}<|endoftext|>


<commit_before>// Package json generates methods for encoding/decoding types to/from JSON.
//
// When used correctly, these methods can easily give a ~200-300% performance
// increase when serializing objects to JSON while also reducing memory usage
// by ~95-99%. For taking advantage of these gains, you must use
// gnd.la/mux/serialize or Context.WriteJson to encode to JSON, since
// json.Marshal won't use these methods correctly and might even have worse
// performance when these methods are implemented.
//
// This is a small benchmark comparing the performance of these JSON encoding
// methods. JSONDirect uses WriteJSON(), JSONSerialize uses
// gnd.la/mux/serialize (which adds some overhead because it also sets the
// Content-Length and Content-Encoding headers and thus must encode into an
// intermediate buffer first), while JSON uses json.Marshal(). All three
// benchmarks write the result to ioutil.Discard.
//
//  BenchmarkJSONDirect	    1000000 1248 ns/op	117.73 MB/s 16 B/op	2 allocs/op
//  BenchmarkJSONSerialize  1000000 1587 ns/op	92.62 MB/s  16 B/op	2 allocs/op
//  BenchmarkJSON	    500000  4583 ns/op	32.07 MB/s  620 B/op	4 allocs/op
//
// Code geerated by this package respects json related struct tags and also
// supports encoding time.Time structs directly (they're encoded as a UTC unix time).
//
// The recommended way use to generate JSON methods for a given package is
// using the gondola command rather than using this package directly.
package json

import (
	"bytes"
	"code.google.com/p/go.tools/go/types"
	"fmt"
	"gnd.la/gen/genutil"
	"gnd.la/log"
	gtypes "gnd.la/types"
	"path/filepath"
	"regexp"
)

type Method struct {
	Key       string
	Name      string
	OmitEmpty bool
}

// Options specify the options used when generating JSON related
// methods.
type Options struct {
	// Wheter to generate a MarshalJSON method. This is false by default
	// because in most cases will result in lower performance when using
	// json.Marshal, since the encoder from encoding/json will revalidate
	// the returned JSON, resulting in a performance loss. Turn this on
	// only if you're using the Methods feature (otherwise you'll get
	// different results when serializing with json.Marshal).
	MarshalJSON bool
	// Methods indicates struct methods which should be included in the JSON
	// output. The key in the map is the type name in the package (e.g.
	// MyStruct not mypackage.MyStruct).
	Methods map[string][]*Method
	// If not nil, only types matching this regexp will be included.
	Include *regexp.Regexp
	// If not nil, types matching this regexp will be excluded.
	Exclude *regexp.Regexp
}

// Gen generates a WriteJSON method and, optionally, MarshalJSON for every
// exported type in the given package. The package might be either an
// absolute path or an import path.
func Gen(pkgName string, opts *Options) error {
	pkg, err := genutil.NewPackage(pkgName)
	if err != nil {
		return err
	}
	var buf bytes.Buffer
	buf.WriteString(fmt.Sprintf("package %s\n\n", pkg.Name()))
	buf.WriteString(genutil.AutogenString())
	buf.WriteString("\nimport (\n")
	imports := []string{"bytes", "io", "runtime", "strconv", "unicode/utf8"}
	for _, v := range imports {
		buf.WriteString(fmt.Sprintf("%q\n", v))
	}
	buf.WriteString(")\n")
	buf.WriteString("var _ = strconv.FormatBool\n")
	var include *regexp.Regexp
	var exclude *regexp.Regexp
	if opts != nil {
		include = opts.Include
		exclude = opts.Exclude
	}
	var methods bytes.Buffer
	for _, v := range pkg.ExportedTypes(include, exclude) {
		methods.Reset()
		if err := jsonMarshal(v, opts, &methods); err != nil {
			log.Warningf("Skipping type %s: %s", v.Obj().Name(), err)
			continue
		}
		buf.WriteString(methods.String())
	}
	buf.WriteString(encode_go)
	buf.WriteString(buffer_go)
	out := filepath.Join(pkg.Dir(), "gen_json.go")
	log.Debugf("Writing autogenerated JSON methods to %s", out)
	return genutil.WriteAutogen(out, buf.Bytes())
}

func jsonMarshal(typ *types.Named, opts *Options, buf *bytes.Buffer) error {
	tname := typ.Obj().Name()
	if _, ok := typ.Underlying().(*types.Struct); ok {
		tname = "*" + tname
	}
	if opts != nil && opts.MarshalJSON {
		buf.WriteString(fmt.Sprintf("func(o %s) MarshalJSON() ([]byte, error) {\n", tname))
		buf.WriteString("var buf bytes.Buffer\n")
		buf.WriteString("_, err := o.WriteJSON(&buf)\n")
		buf.WriteString("return buf.Bytes(), err\n")
		buf.WriteString("}\n\n")
	}
	buf.WriteString(fmt.Sprintf("func(o %s) WriteJSON(w io.Writer) (int, error) {\n", tname))
	buf.WriteString("buf := jsonGetBuffer()\n")
	if err := jsonValue(typ, nil, "o", opts, buf); err != nil {
		return err
	}
	buf.WriteString("n, err := w.Write(buf.Bytes())\n")
	buf.WriteString("jsonPutBuffer(buf)\n")
	buf.WriteString("return n, err\n")
	buf.WriteString("}\n\n")
	return nil
}

func jsonStruct(st *types.Struct, p types.Type, name string, opts *Options, buf *bytes.Buffer) error {
	buf.WriteString("buf.WriteByte('{')\n")
	count := st.NumFields()
	hasFields := false
	for ii := 0; ii < count; ii++ {
		field := st.Field(ii)
		if field.IsExported() {
			key := field.Name()
			omitEmpty := false
			tag := st.Tag(ii)
			if gtag := gtypes.NewStringTagNamed(tag, "json"); gtag != nil {
				if n := gtag.Name(); n != "" {
					key = n
				}
				omitEmpty = gtag.Has("omitempty")
			}
			if key != "-" {
				if hasFields {
					buf.WriteString("buf.WriteByte(',')\n")
				}
				hasFields = true
				if err := jsonField(field, key, name+"."+field.Name(), omitEmpty, opts, buf); err != nil {
					return err
				}
			}
		}
	}
	if opts != nil {
		if named, ok := p.(*types.Named); ok {
			methods := opts.Methods[named.Obj().Name()]
			count := named.NumMethods()
			for _, v := range methods {
				found := false
				for ii := 0; ii < count; ii++ {
					fn := named.Method(ii)
					if fn.Name() == v.Name {
						found = true
						signature := fn.Type().(*types.Signature)
						if p := signature.Params(); p != nil || p.Len() > 0 {
							return fmt.Errorf("method %s on type %s requires arguments", v.Name, named.Obj().Name())
						}
						res := signature.Results()
						if res == nil || res.Len() != 1 {
							return fmt.Errorf("method %s on type %s must return exactly one value", v.Name, named.Obj().Name())
						}
						if hasFields {
							buf.WriteString("buf.WriteByte(',')\n")
						}
						hasFields = true
						if err := jsonField(res.At(0), v.Key, name+"."+v.Name+"()", v.OmitEmpty, opts, buf); err != nil {
							return err
						}
						break
					}
				}
				if !found {
					return fmt.Errorf("type %s does not have method %s", named.Obj().Name(), v.Name)
				}
			}
		}
	}
	buf.WriteString("buf.WriteByte('}')\n")
	return nil
}

func jsonSlice(sl *types.Slice, p types.Type, name string, opts *Options, buf *bytes.Buffer) error {
	buf.WriteString("buf.WriteByte('[')\n")
	buf.WriteString(fmt.Sprintf("for ii, v := range %s {\n", name))
	buf.WriteString("if ii > 0 {\n")
	buf.WriteString("buf.WriteByte(',')\n")
	buf.WriteString("}\n")
	if err := jsonValue(sl.Elem(), nil, "v", opts, buf); err != nil {
		return err
	}
	buf.WriteString("}\n")
	buf.WriteString("buf.WriteByte(']')\n")
	return nil
}

func jsonField(field *types.Var, key string, name string, omitEmpty bool, opts *Options, buf *bytes.Buffer) error {
	// TODO: omitEmpty
	buf.WriteString(fmt.Sprintf("buf.WriteString(%q)\n", fmt.Sprintf("%q", key)))
	buf.WriteString("buf.WriteByte(':')\n")
	if err := jsonValue(field.Type(), nil, name, opts, buf); err != nil {
		return err
	}
	return nil
}

func jsonValue(vtype types.Type, ptype types.Type, name string, opts *Options, buf *bytes.Buffer) error {
	switch typ := vtype.(type) {
	case *types.Basic:
		k := typ.Kind()
		_, isPointer := ptype.(*types.Pointer)
		if isPointer {
			name = "*" + name
		}
		switch k {
		case types.Bool:
			buf.WriteString(fmt.Sprintf("buf.WriteString(strconv.FormatBool(%s))\n", name))
		case types.Int, types.Int8, types.Int16, types.Int32, types.Int64:
			buf.WriteString(fmt.Sprintf("buf.WriteString(strconv.FormatInt(int64(%s), 10))\n", name))
		case types.Uint, types.Uint8, types.Uint16, types.Uint32, types.Uint64:
			buf.WriteString(fmt.Sprintf("buf.WriteString(strconv.FormatUint(uint64(%s), 10))\n", name))
		case types.Float32, types.Float64:
			bitSize := 64
			if k == types.Float32 {
				bitSize = 32
			}
			buf.WriteString(fmt.Sprintf("buf.WriteString(strconv.FormatFloat(float64(%s), 'g', -1, %d))\n", name, bitSize))
		case types.String:
			buf.WriteString(fmt.Sprintf("jsonEncodeString(buf, string(%s))\n", name))
		default:
			return fmt.Errorf("can't encode basic kind %v", typ.Kind())
		}
	case *types.Named:
		if typ.Obj().Pkg().Name() == "time" && typ.Obj().Name() == "Time" {
			buf.WriteString(fmt.Sprintf("buf.WriteString(strconv.FormatInt(%s.UTC().Unix(), 10))\n", name))
		} else {
			if err := jsonValue(typ.Underlying(), typ, name, opts, buf); err != nil {
				return err
			}
		}
	case *types.Slice:
		if err := jsonSlice(typ, ptype, name, opts, buf); err != nil {
			return err
		}
	case *types.Struct:
		if err := jsonStruct(typ, ptype, name, opts, buf); err != nil {
			return err
		}
	case *types.Pointer:
		buf.WriteString(fmt.Sprintf("if %s == nil {\n", name))
		buf.WriteString("buf.WriteString(\"null\")\n")
		buf.WriteString("} else {\n")
		if err := jsonValue(typ.Elem(), typ, name, opts, buf); err != nil {
			return err
		}
		buf.WriteString("}\n")
	default:
		return fmt.Errorf("can't encode type %T %v (%T)", typ, typ, typ.Underlying())
	}
	return nil
}
<commit_msg>Fix typo in comment<commit_after>// Package json generates methods for encoding/decoding types to/from JSON.
//
// When used correctly, these methods can easily give a ~200-300% performance
// increase when serializing objects to JSON while also reducing memory usage
// by ~95-99%. For taking advantage of these gains, you must use
// gnd.la/mux/serialize or Context.WriteJson to encode to JSON, since
// json.Marshal won't use these methods correctly and might even have worse
// performance when these methods are implemented.
//
// This is a small benchmark comparing the performance of these JSON encoding
// methods. JSONDirect uses WriteJSON(), JSONSerialize uses
// gnd.la/mux/serialize (which adds some overhead because it also sets the
// Content-Length and Content-Encoding headers and thus must encode into an
// intermediate buffer first), while JSON uses json.Marshal(). All three
// benchmarks write the result to ioutil.Discard.
//
//  BenchmarkJSONDirect	    1000000 1248 ns/op	117.73 MB/s 16 B/op	2 allocs/op
//  BenchmarkJSONSerialize  1000000 1587 ns/op	92.62 MB/s  16 B/op	2 allocs/op
//  BenchmarkJSON	    500000  4583 ns/op	32.07 MB/s  620 B/op	4 allocs/op
//
// Code generated by this package respects json related struct tags and also
// supports encoding time.Time structs directly (they're encoded as a UTC unix time).
//
// The recommended way use to generate JSON methods for a given package is
// using the gondola command rather than using this package directly.
package json

import (
	"bytes"
	"code.google.com/p/go.tools/go/types"
	"fmt"
	"gnd.la/gen/genutil"
	"gnd.la/log"
	gtypes "gnd.la/types"
	"path/filepath"
	"regexp"
)

type Method struct {
	Key       string
	Name      string
	OmitEmpty bool
}

// Options specify the options used when generating JSON related
// methods.
type Options struct {
	// Wheter to generate a MarshalJSON method. This is false by default
	// because in most cases will result in lower performance when using
	// json.Marshal, since the encoder from encoding/json will revalidate
	// the returned JSON, resulting in a performance loss. Turn this on
	// only if you're using the Methods feature (otherwise you'll get
	// different results when serializing with json.Marshal).
	MarshalJSON bool
	// Methods indicates struct methods which should be included in the JSON
	// output. The key in the map is the type name in the package (e.g.
	// MyStruct not mypackage.MyStruct).
	Methods map[string][]*Method
	// If not nil, only types matching this regexp will be included.
	Include *regexp.Regexp
	// If not nil, types matching this regexp will be excluded.
	Exclude *regexp.Regexp
}

// Gen generates a WriteJSON method and, optionally, MarshalJSON for every
// exported type in the given package. The package might be either an
// absolute path or an import path.
func Gen(pkgName string, opts *Options) error {
	pkg, err := genutil.NewPackage(pkgName)
	if err != nil {
		return err
	}
	var buf bytes.Buffer
	buf.WriteString(fmt.Sprintf("package %s\n\n", pkg.Name()))
	buf.WriteString(genutil.AutogenString())
	buf.WriteString("\nimport (\n")
	imports := []string{"bytes", "io", "runtime", "strconv", "unicode/utf8"}
	for _, v := range imports {
		buf.WriteString(fmt.Sprintf("%q\n", v))
	}
	buf.WriteString(")\n")
	buf.WriteString("var _ = strconv.FormatBool\n")
	var include *regexp.Regexp
	var exclude *regexp.Regexp
	if opts != nil {
		include = opts.Include
		exclude = opts.Exclude
	}
	var methods bytes.Buffer
	for _, v := range pkg.ExportedTypes(include, exclude) {
		methods.Reset()
		if err := jsonMarshal(v, opts, &methods); err != nil {
			log.Warningf("Skipping type %s: %s", v.Obj().Name(), err)
			continue
		}
		buf.WriteString(methods.String())
	}
	buf.WriteString(encode_go)
	buf.WriteString(buffer_go)
	out := filepath.Join(pkg.Dir(), "gen_json.go")
	log.Debugf("Writing autogenerated JSON methods to %s", out)
	return genutil.WriteAutogen(out, buf.Bytes())
}

func jsonMarshal(typ *types.Named, opts *Options, buf *bytes.Buffer) error {
	tname := typ.Obj().Name()
	if _, ok := typ.Underlying().(*types.Struct); ok {
		tname = "*" + tname
	}
	if opts != nil && opts.MarshalJSON {
		buf.WriteString(fmt.Sprintf("func(o %s) MarshalJSON() ([]byte, error) {\n", tname))
		buf.WriteString("var buf bytes.Buffer\n")
		buf.WriteString("_, err := o.WriteJSON(&buf)\n")
		buf.WriteString("return buf.Bytes(), err\n")
		buf.WriteString("}\n\n")
	}
	buf.WriteString(fmt.Sprintf("func(o %s) WriteJSON(w io.Writer) (int, error) {\n", tname))
	buf.WriteString("buf := jsonGetBuffer()\n")
	if err := jsonValue(typ, nil, "o", opts, buf); err != nil {
		return err
	}
	buf.WriteString("n, err := w.Write(buf.Bytes())\n")
	buf.WriteString("jsonPutBuffer(buf)\n")
	buf.WriteString("return n, err\n")
	buf.WriteString("}\n\n")
	return nil
}

func jsonStruct(st *types.Struct, p types.Type, name string, opts *Options, buf *bytes.Buffer) error {
	buf.WriteString("buf.WriteByte('{')\n")
	count := st.NumFields()
	hasFields := false
	for ii := 0; ii < count; ii++ {
		field := st.Field(ii)
		if field.IsExported() {
			key := field.Name()
			omitEmpty := false
			tag := st.Tag(ii)
			if gtag := gtypes.NewStringTagNamed(tag, "json"); gtag != nil {
				if n := gtag.Name(); n != "" {
					key = n
				}
				omitEmpty = gtag.Has("omitempty")
			}
			if key != "-" {
				if hasFields {
					buf.WriteString("buf.WriteByte(',')\n")
				}
				hasFields = true
				if err := jsonField(field, key, name+"."+field.Name(), omitEmpty, opts, buf); err != nil {
					return err
				}
			}
		}
	}
	if opts != nil {
		if named, ok := p.(*types.Named); ok {
			methods := opts.Methods[named.Obj().Name()]
			count := named.NumMethods()
			for _, v := range methods {
				found := false
				for ii := 0; ii < count; ii++ {
					fn := named.Method(ii)
					if fn.Name() == v.Name {
						found = true
						signature := fn.Type().(*types.Signature)
						if p := signature.Params(); p != nil || p.Len() > 0 {
							return fmt.Errorf("method %s on type %s requires arguments", v.Name, named.Obj().Name())
						}
						res := signature.Results()
						if res == nil || res.Len() != 1 {
							return fmt.Errorf("method %s on type %s must return exactly one value", v.Name, named.Obj().Name())
						}
						if hasFields {
							buf.WriteString("buf.WriteByte(',')\n")
						}
						hasFields = true
						if err := jsonField(res.At(0), v.Key, name+"."+v.Name+"()", v.OmitEmpty, opts, buf); err != nil {
							return err
						}
						break
					}
				}
				if !found {
					return fmt.Errorf("type %s does not have method %s", named.Obj().Name(), v.Name)
				}
			}
		}
	}
	buf.WriteString("buf.WriteByte('}')\n")
	return nil
}

func jsonSlice(sl *types.Slice, p types.Type, name string, opts *Options, buf *bytes.Buffer) error {
	buf.WriteString("buf.WriteByte('[')\n")
	buf.WriteString(fmt.Sprintf("for ii, v := range %s {\n", name))
	buf.WriteString("if ii > 0 {\n")
	buf.WriteString("buf.WriteByte(',')\n")
	buf.WriteString("}\n")
	if err := jsonValue(sl.Elem(), nil, "v", opts, buf); err != nil {
		return err
	}
	buf.WriteString("}\n")
	buf.WriteString("buf.WriteByte(']')\n")
	return nil
}

func jsonField(field *types.Var, key string, name string, omitEmpty bool, opts *Options, buf *bytes.Buffer) error {
	// TODO: omitEmpty
	buf.WriteString(fmt.Sprintf("buf.WriteString(%q)\n", fmt.Sprintf("%q", key)))
	buf.WriteString("buf.WriteByte(':')\n")
	if err := jsonValue(field.Type(), nil, name, opts, buf); err != nil {
		return err
	}
	return nil
}

func jsonValue(vtype types.Type, ptype types.Type, name string, opts *Options, buf *bytes.Buffer) error {
	switch typ := vtype.(type) {
	case *types.Basic:
		k := typ.Kind()
		_, isPointer := ptype.(*types.Pointer)
		if isPointer {
			name = "*" + name
		}
		switch k {
		case types.Bool:
			buf.WriteString(fmt.Sprintf("buf.WriteString(strconv.FormatBool(%s))\n", name))
		case types.Int, types.Int8, types.Int16, types.Int32, types.Int64:
			buf.WriteString(fmt.Sprintf("buf.WriteString(strconv.FormatInt(int64(%s), 10))\n", name))
		case types.Uint, types.Uint8, types.Uint16, types.Uint32, types.Uint64:
			buf.WriteString(fmt.Sprintf("buf.WriteString(strconv.FormatUint(uint64(%s), 10))\n", name))
		case types.Float32, types.Float64:
			bitSize := 64
			if k == types.Float32 {
				bitSize = 32
			}
			buf.WriteString(fmt.Sprintf("buf.WriteString(strconv.FormatFloat(float64(%s), 'g', -1, %d))\n", name, bitSize))
		case types.String:
			buf.WriteString(fmt.Sprintf("jsonEncodeString(buf, string(%s))\n", name))
		default:
			return fmt.Errorf("can't encode basic kind %v", typ.Kind())
		}
	case *types.Named:
		if typ.Obj().Pkg().Name() == "time" && typ.Obj().Name() == "Time" {
			buf.WriteString(fmt.Sprintf("buf.WriteString(strconv.FormatInt(%s.UTC().Unix(), 10))\n", name))
		} else {
			if err := jsonValue(typ.Underlying(), typ, name, opts, buf); err != nil {
				return err
			}
		}
	case *types.Slice:
		if err := jsonSlice(typ, ptype, name, opts, buf); err != nil {
			return err
		}
	case *types.Struct:
		if err := jsonStruct(typ, ptype, name, opts, buf); err != nil {
			return err
		}
	case *types.Pointer:
		buf.WriteString(fmt.Sprintf("if %s == nil {\n", name))
		buf.WriteString("buf.WriteString(\"null\")\n")
		buf.WriteString("} else {\n")
		if err := jsonValue(typ.Elem(), typ, name, opts, buf); err != nil {
			return err
		}
		buf.WriteString("}\n")
	default:
		return fmt.Errorf("can't encode type %T %v (%T)", typ, typ, typ.Underlying())
	}
	return nil
}
<|endoftext|>


<commit_before><commit_msg>make watcher work with >1 repo<commit_after><|endoftext|>


<commit_before>package controller

import (
	"github.com/eaciit/colony-core/v0"
	"github.com/eaciit/colony-manager/helper"
	"github.com/eaciit/knot/knot.v1"
)

type DataGrabberController struct {
	App
}

func CreateDataGrabberController(s *knot.Server) *DataGrabberController {
	var controller = new(DataGrabberController)
	controller.Server = s
	return controller
}

func (d *DataGrabberController) SaveDataGrabber(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson

	payload := new(colonycore.DataGrabber)
	err := r.GetPayload(payload)
	if err != nil {
		return helper.CreateResult(false, nil, err.Error())
	}

	err = colonycore.Save(payload)
	if err != nil {
		return helper.CreateResult(false, nil, err.Error())
	}

	return helper.CreateResult(true, payload, "")
}

func (d *DataGrabberController) SelectDataGrabber(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson

	payload := new(colonycore.DataGrabber)
	err := r.GetPayload(payload)
	if err != nil {
		return helper.CreateResult(false, nil, err.Error())
	}

	err = colonycore.Get(payload, payload.ID)

	return helper.CreateResult(true, payload, "")
}

func (d *DataGrabberController) GetDataGrabber(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson

	data := []colonycore.DataGrabber{}
	cursor, err := colonycore.Find(new(colonycore.DataGrabber), nil)
	if err != nil {
		return helper.CreateResult(false, nil, err.Error())
	}

	err = cursor.Fetch(&data, 0, false)
	if err != nil {
		return helper.CreateResult(false, nil, err.Error())
	}
	defer cursor.Close()

	return helper.CreateResult(true, data, "")
}

func (d *DataGrabberController) RemoveDataGrabber(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson

	payload := new(colonycore.DataGrabber)
	err := r.GetPayload(payload)
	if err != nil {
		return helper.CreateResult(false, nil, err.Error())
	}

	err = colonycore.Delete(payload)
	if err != nil {
		return helper.CreateResult(false, nil, err.Error())
	}

	return helper.CreateResult(true, nil, "")
}
<commit_msg>just have item material needed (id, datasoucre, datadestination, param connection). other function (connection, build/run query) is not work.<commit_after>package controller

import (
	"github.com/eaciit/colony-core/v0"
	"github.com/eaciit/colony-manager/helper"
	"github.com/eaciit/knot/knot.v1"
	"github.com/eaciit/dbox"
	"fmt"
	"strings"
	"errors"
	//~ "encoding/jsson"
)

type DataGrabberController struct {
	App
}

func CreateDataGrabberController(s *knot.Server) *DataGrabberController {
	var controller = new(DataGrabberController)
	controller.Server = s
	return controller
}

func (d *DataGrabberController) SaveDataGrabber(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson

	payload := new(colonycore.DataGrabber)
	err := r.GetPayload(payload)
	if err != nil {
		return helper.CreateResult(false, nil, err.Error())
	}

	err = colonycore.Save(payload)
	if err != nil {
		return helper.CreateResult(false, nil, err.Error())
	}

	return helper.CreateResult(true, payload, "")
}

func (d *DataGrabberController) SelectDataGrabber(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson

	payload := new(colonycore.DataGrabber)
	err := r.GetPayload(payload)
	if err != nil {
		return helper.CreateResult(false, nil, err.Error())
	}

	err = colonycore.Get(payload, payload.ID)

	return helper.CreateResult(true, payload, "")
}

func (d *DataGrabberController) GetDataGrabber(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson

	data := []colonycore.DataGrabber{}
	cursor, err := colonycore.Find(new(colonycore.DataGrabber), nil)
	if err != nil {
		return helper.CreateResult(false, nil, err.Error())
	}

	err = cursor.Fetch(&data, 0, false)
	if err != nil {
		return helper.CreateResult(false, nil, err.Error())
	}
	defer cursor.Close()

	return helper.CreateResult(true, data, "")
}

func (d *DataGrabberController) RemoveDataGrabber(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson

	payload := new(colonycore.DataGrabber)
	err := r.GetPayload(payload)
	if err != nil {
		return helper.CreateResult(false, nil, err.Error())
	}

	err = colonycore.Delete(payload)
	if err != nil {
		return helper.CreateResult(false, nil, err.Error())
	}

	return helper.CreateResult(true, nil, "")
}

func (d *DataGrabberController) RunDataGrabber(r *knot.WebContext) interface{} {
	//~ dataInput := json.Marshal(`{"data":{"_id":"adsfasdf","DataSourceOrigin":"ds1453549917327","DataSourceDestination":"ds1453549868712","IgnoreFieldsOrigin":[],"IgnoreFieldsDestination":["UARigSequenceId"],"Map":[{"FieldOrigin":"_id","FieldDestination":"UARigSequenceId"},{"FieldOrigin":"_id","FieldDestination":"AssignTOOps"}]},"message":"","success":true}`)
	//~ r.Config.OutputType = knot.OutputJson

	payload := map[string]string{"_id":"adsfasdf",}
	_id := payload["_id"]
	dg := new(colonycore.DataGrabber)
	colonycore.Get(dg, _id)

	dso := new(colonycore.DataSource)
	idDataSourceOrigin := dg.DataSourceOrigin
	colonycore.Get(dso, idDataSourceOrigin)

	dsd := new(colonycore.DataSource)
	idDataSourceDestination := dg.DataSourceDestination
	colonycore.Get(dsd, idDataSourceDestination)

	//~ conn := new(colonycore.Connection)
	//~ idDataSourceDestination := dg.DataSourceDestination
	//~ colonycore.Get(dsd, idDataSourceDestination)

	dataDS, _, conn, query, metaSave, err := d.connectingDataSource(idDataSourceOrigin)

	//~ DataSourceController{}.GetConnections()

	fmt.Printf("\n========================\n")
	//~ fmt.Printf("%v",r.Config.OutputType)
	//~ fmt.Printf("\n========================\n")
	//~ fmt.Printf("%v",x)
	//~ fmt.Printf("\n========================\n")
	fmt.Printf("%v",dso)
	fmt.Printf("\n========================\n")
	fmt.Printf("%v",dsd)
	fmt.Printf("\n========================\n")

	return helper.CreateResult(true, payload, "")
}

func (d *DataSourceController) checkIfDriverIsSupported(driver string) error {
	supportedDrivers := "mongo mysql weblink"

	if !strings.Contains(supportedDrivers, driver) {
		drivers := strings.Replace(supportedDrivers, " ", ", ", -1)
		return errors.New("Currently tested driver is only " + drivers)
	}

	return nil
}

func (d *DataGrabberController) connectingDataSource(_id string) (*colonycore.DataSource, *colonycore.Connection, dbox.IConnection, dbox.IQuery, MetaSave, error) {
	dataDS := new(colonycore.DataSource)
	err := colonycore.Get(dataDS, _id)
	if err != nil {
		return nil, nil, nil, nil, MetaSave{}, err
	}

	dataConn := new(colonycore.Connection)
	err = colonycore.Get(dataConn, dataDS.ConnectionID)
	if err != nil {
		return nil, nil, nil, nil, MetaSave{}, err
	}

	if err := d.checkIfDriverIsSupported(dataConn.Driver); err != nil {
		return nil, nil, nil, nil, MetaSave{}, err
	}

	connection, err := helper.ConnectUsingDataConn(dataConn).Connect()
	if err != nil {
		return nil, nil, nil, nil, MetaSave{}, err
	}

	query, metaSave := d.parseQuery(connection.NewQuery(), dataDS.QueryInfo)
	return dataDS, dataConn, connection, query, metaSave, nil
}
<|endoftext|>